Skip to contentSkip to conversation

add hypercall option to temporarily pin a vcpu

PATCH Superseded Juergen Gross 7 participants 26 messages Mar 1, 2016 – Mar 3, 2016 1 message not in the archive

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover add hypercall option to temporarily pin a vcpu

Some hardware (e.g. Dell studio 1555 laptops) require SMIs to be called on physical cpu 0 only. Linux drivers like dcdbas or i8k try to achieve this by pinning the running thread to cpu 0, but in Dom0 this is not enough: the vcpu must be pinned to physical cpu 0 via Xen, too.

This patch series adds a stable hypercall option to achieve this.

Changes in V2:
- add patch 1 to silence messages on suspend/resume
- add patch 3 to handle EBUSY case when removing cpu from cpupool
- limit operation to hardware domain as suggested by Jan Beulich
- some style issues corrected as requested by Jan Beulich
- use fixed width types in interface as requested by Jan Beulich
- add compat layer checking as requested by Jan Beulich

Juergen Gross (3):
xen: silence affinity messages on suspend/resume
xen: add hypercall option to temporarily pin a vcpu
libxc: do some retries in xc_cpupool_removecpu() for EBUSY case

 tools/libxc/xc_cpupool.c     | 13 +++++-
 xen/common/compat/schedule.c |  4 ++
 xen/common/schedule.c        | 95 ++++++++++++++++++++++++++++++++++++++++----
 xen/include/public/sched.h   | 17 ++++++++
 xen/include/xlat.lst         |  1 +
 5 files changed, 122 insertions(+), 8 deletions(-)
Signature · 7 lines
2.6.2


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#1 lore raw

PATCH v2 1/3 xen: silence affinity messages on suspend/resume

When taking cpus offline for suspend or bringing them online on resume again the scheduler might issue debug messages when temporarily breaking vcpu affinity or restoring the original affinity settings.

The resume message can be removed completely, while the message when breaking affinity should only be issued if the breakage is permanent.

---
 xen/common/schedule.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
xen/common/schedule.c +2 -3
index 434dcfc..b0d4b18 100644
@@ -615,7 +615,6 @@ void restore_vcpu_affinity(struct domain *d)
 
         if ( v->affinity_broken )
         {
-            printk(XENLOG_DEBUG "Restoring affinity for %pv\n", v);
             cpumask_copy(v->cpu_hard_affinity, v->cpu_hard_affinity_saved);
             v->affinity_broken = 0;
         }
@@ -670,14 +669,14 @@ int cpu_disable_scheduler(unsigned int cpu)
             if ( cpumask_empty(&online_affinity) &&
                  cpumask_test_cpu(cpu, v->cpu_hard_affinity) )
             {
-                printk(XENLOG_DEBUG "Breaking affinity for %pv\n", v);
-
                 if (system_state == SYS_STATE_suspend)
                 {
                     cpumask_copy(v->cpu_hard_affinity_saved,
                                  v->cpu_hard_affinity);
                     v->affinity_broken = 1;
                 }
+                else
+                    printk(XENLOG_DEBUG "Breaking affinity for %pv\n", v);
 
                 cpumask_setall(v->cpu_hard_affinity);
             }
Signature · 7 lines
2.6.2


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Acked-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] xen: silence affinity messages on suspend/resume

Quoted 11 lines · Juergen Gross

When taking cpus offline for suspend or bringing them online on resume
again the scheduler might issue debug messages when temporarily breaking vcpu affinity or restoring the original affinity settings.

The resume message can be removed completely, while the message when breaking affinity should only be issued if the breakage is permanent.

Thanks and Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#3 lore raw

PATCH v2 2/3 xen: add hypercall option to temporarily pin a vcpu

In reply to Juergen Gross

Some hardware (e.g. Dell studio 1555 laptops) require SMIs to be called on physical cpu 0 only. Linux drivers like dcdbas or i8k try to achieve this by pinning the running thread to cpu 0, but in Dom0 this is not enough: the vcpu must be pinned to physical cpu 0 via Xen, too.

Add a stable hypercall option SCHEDOP_pin_temp to the sched_op hypercall to achieve this. It is taking a physical cpu number as parameter. If pinning is possible (the calling domain has the privilege to make the call and the cpu is available in the domain's cpupool) the calling vcpu is pinned to the specified cpu. The old cpu affinity is saved. To undo the temporary pinning a cpu -1 is specified. This will restore the original cpu affinity for the vcpu.

---
V2: - limit operation to hardware domain as suggested by Jan Beulich
- some style issues corrected as requested by Jan Beulich
- use fixed width types in interface as requested by Jan Beulich
- add compat layer checking as requested by Jan Beulich

---
 xen/common/compat/schedule.c |  4 ++
 xen/common/schedule.c        | 92 +++++++++++++++++++++++++++++++++++++++++---
 xen/include/public/sched.h   | 17 ++++++++
 xen/include/xlat.lst         |  1 +
 4 files changed, 109 insertions(+), 5 deletions(-)
xen/common/compat/schedule.c +4 -0
index 812c550..73b0f01 100644
@@ -10,6 +10,10 @@
 
 #define do_sched_op compat_sched_op
 
+#define xen_sched_pin_temp sched_pin_temp
+CHECK_sched_pin_temp;
+#undef xen_sched_pin_temp
+
 #define xen_sched_shutdown sched_shutdown
 CHECK_sched_shutdown;
 #undef xen_sched_shutdown
xen/common/schedule.c +87 -5

132 lines not on this page · raw message

xen/include/public/sched.h +17 -0
index 2219696..a0ce5a6 100644
@@ -118,6 +118,17 @@
  * With id != 0 and timeout != 0, poke watchdog timer and set new timeout.
  */
 #define SCHEDOP_watchdog    6
+
+/*
+ * Temporarily pin the current vcpu to one physical cpu or undo that pinning.
+ * @arg == pointer to sched_pin_temp_t structure.
+ *
+ * Setting pcpu to -1 will undo a previous temporary pinning and restore the
+ * previous cpu affinity. The temporary aspect of the pinning isn't enforced
+ * by the hypervisor.
+ * This call is allowed for the hardware domain only.
+ */
+#define SCHEDOP_pin_temp    7
 /* ` } */
 
 struct sched_shutdown {
@@ -148,6 +159,12 @@ struct sched_watchdog {
 typedef struct sched_watchdog sched_watchdog_t;
 DEFINE_XEN_GUEST_HANDLE(sched_watchdog_t);
 
+struct sched_pin_temp {
+    int32_t pcpu;
+};
+typedef struct sched_pin_temp sched_pin_temp_t;
+DEFINE_XEN_GUEST_HANDLE(sched_pin_temp_t);
+
 /*
  * Reason codes for SCHEDOP_shutdown. These may be interpreted by control
  * software to determine the appropriate action. For the most part, Xen does
xen/include/xlat.lst +1 -0
index fda1137..52c7233 100644
@@ -104,6 +104,7 @@
 ?	pmu_data			pmu.h
 ?	pmu_params			pmu.h
 !	sched_poll			sched.h
+?	sched_pin_temp			sched.h
 ?	sched_remote_shutdown		sched.h
 ?	sched_shutdown			sched.h
 ?	tmem_oid			tmem.h
Signature · 7 lines
2.6.2


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#4 lore raw

Re: [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 01.03.16 at 10:02, <[email protected]> wrote:
xen/common/schedule.c +3 -1
@@ -752,14 +766,20 @@ static int vcpu_set_affinity(
     struct vcpu *v, const cpumask_t *affinity, cpumask_t *which)
 {
     spinlock_t *lock;
+    int ret = 0;
 
     lock = vcpu_schedule_lock_irq(v);
 
-    cpumask_copy(which, affinity);
+    if ( v->affinity_broken )
+        ret = -EBUSY;

xen/common/schedule.c static int vcpu_set_affinity( · line 781

Quoted 10 lines
xen/common/schedule.c +6 -3
+    else
+    {
+        cpumask_copy(which, affinity);
 
-    /* Always ask the scheduler to re-evaluate placement
-     * when changing the affinity */
-    set_bit(_VPF_migrating, &v->pause_flags);
+        /* Always ask the scheduler to re-evaluate placement
+         * when changing the affinity */
+        set_bit(_VPF_migrating, &v->pause_flags);

When you touch code like this, would it be possible to at once fix the coding style issues it (the comment in this case) has?

xen/common/schedule.c void watchdog_domain_destroy(struct domain *d) · line 1001

Quoted 5 lines
xen/common/schedule.c +1 -0
@@ -978,6 +998,51 @@ void watchdog_domain_destroy(struct domain *d)
         kill_timer(&d->watchdog_timer[i]);
 }
 
+static long do_pin_temp(int cpu)

As expressed before, throughout this patch I dislike the "temp" naming, when the temporary nature of this operation isn't being enforced by anything.

Apart from that I (vaguely) recall there having been previous suggestions in the direction of (temporary), which have got rejected.

On both points I think we need to have input from the scheduler maintainers.

xen/common/schedule.c void watchdog_domain_destroy(struct domain *d) · line 1005

Quoted 4 lines
xen/common/schedule.c +4 -0
+{
+    struct vcpu *v = current;
+    spinlock_t *lock;
+    long ret = -EINVAL;

"int" seems completely sufficient for both the variable and the function return type.

Quoted 5 lines
xen/common/schedule.c +1 -0
@@ -1087,6 +1152,23 @@ ret_t do_sched_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         break;
     }
 
+    case SCHEDOP_pin_temp:

xen/common/schedule.c ret_t do_sched_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 1165

Quoted 10 lines
xen/common/schedule.c +10 -0
+    {
+        struct sched_pin_temp sched_pin_temp;
+
+        ret = -EFAULT;
+        if ( copy_from_guest(&sched_pin_temp, arg, 1) )
+            break;
+
+        ret = -EPERM;
+        if ( !is_hardware_domain(current->domain) )
+            break;

I'd generally suggest swapping these two.

Quoted 4 lines
xen/include/public/sched.h +0 -0
@@ -118,6 +118,17 @@
  * With id != 0 and timeout != 0, poke watchdog timer and set new timeout.

xen/include/public/sched.h line 128

Quoted 10 lines
xen/include/public/sched.h +8 -0
  */
 #define SCHEDOP_watchdog    6
+
+/*
+ * Temporarily pin the current vcpu to one physical cpu or undo that pinning.
+ * @arg == pointer to sched_pin_temp_t structure.
+ *
+ * Setting pcpu to -1 will undo a previous temporary pinning and restore the
+ * previous cpu affinity. The temporary aspect of the pinning isn't enforced
+ * by the hypervisor.

This comment is now out of sync with the code, since you now accept any negative CPU number as "undo" request.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#5 lore raw

Re: [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

In reply to Juergen Gross
Quoted 6 lines · Juergen Gross

Some hardware (e.g. Dell studio 1555 laptops) require SMIs to be called on physical cpu 0 only. Linux drivers like dcdbas or i8k try to achieve this by pinning the running thread to cpu 0, but in Dom0 this is not enough: the vcpu must be pinned to physical cpu 0 via Xen, too.

Quoted 7 lines

Add a stable hypercall option SCHEDOP_pin_temp to the sched_op hypercall to achieve this. It is taking a physical cpu number as parameter. If pinning is possible (the calling domain has the privilege to make the call and the cpu is available in the domain's cpupool) the calling vcpu is pinned to the specified cpu. The old cpu affinity is saved. To undo the temporary pinning a cpu -1 is specified. This will restore the original cpu affinity for the vcpu.

I suggest SCHEDOP_pin_override as a name.

David

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#6 lore raw

Re: [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

Quoted 15 lines · David Vrabel
Quoted 6 lines · Juergen Gross

Some hardware (e.g. Dell studio 1555 laptops) require SMIs to be called on physical cpu 0 only. Linux drivers like dcdbas or i8k try to achieve this by pinning the running thread to cpu 0, but in Dom0 this is not enough: the vcpu must be pinned to physical cpu 0 via Xen, too.

Quoted 7 lines

Add a stable hypercall option SCHEDOP_pin_temp to the sched_op hypercall to achieve this. It is taking a physical cpu number as parameter. If pinning is possible (the calling domain has the privilege to make the call and the cpu is available in the domain's cpupool) the calling vcpu is pinned to the specified cpu. The old cpu affinity is saved. To undo the temporary pinning a cpu -1 is specified. This will restore the original cpu affinity for the vcpu.

Quoted 1 line

I suggest SCHEDOP_pin_override as a name.

I'm fine with that. Any objections?

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#7 lore raw

Re: [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

Quoted 21 lines · Juergen Gross
Quoted 20 lines · David Vrabel
Quoted 18 lines · Juergen Gross

Some hardware (e.g. Dell studio 1555 laptops) require SMIs to be called on physical cpu 0 only. Linux drivers like dcdbas or i8k try
to achieve this by pinning the running thread to cpu 0, but in Dom0
this is not enough: the vcpu must be pinned to physical cpu 0 via Xen, too.

Add a stable hypercall option SCHEDOP_pin_temp to the sched_op hypercall to achieve this. It is taking a physical cpu number as parameter. If pinning is possible (the calling domain has the privilege to make the call and the cpu is available in the domain's
cpupool) the calling vcpu is pinned to the specified cpu. The old cpu affinity is saved. To undo the temporary pinning a cpu -1 is specified. This will restore the original cpu affinity for the vcpu.

Quoted 4 lines
Quoted 1 line

I suggest SCHEDOP_pin_override as a name.

I'm fine with that. Any objections?

Not at all. I actually like it a lot.

Thanks and Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#8 lore raw

Re: [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

Quoted 22 lines · Dario Faggioli
Quoted 21 lines · Juergen Gross
Quoted 20 lines · David Vrabel
Quoted 18 lines · Juergen Gross

Some hardware (e.g. Dell studio 1555 laptops) require SMIs to be called on physical cpu 0 only. Linux drivers like dcdbas or i8k try
to achieve this by pinning the running thread to cpu 0, but in Dom0
this is not enough: the vcpu must be pinned to physical cpu 0 via Xen, too.

Add a stable hypercall option SCHEDOP_pin_temp to the sched_op hypercall to achieve this. It is taking a physical cpu number as parameter. If pinning is possible (the calling domain has the privilege to make the call and the cpu is available in the domain's
cpupool) the calling vcpu is pinned to the specified cpu. The old cpu affinity is saved. To undo the temporary pinning a cpu -1 is specified. This will restore the original cpu affinity for the vcpu.

Quoted 5 lines
Quoted 4 lines
Quoted 1 line

I suggest SCHEDOP_pin_override as a name.

I'm fine with that. Any objections?

Not at all. I actually like it a lot.

+1 to the name.

-George

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

A message that is not in the archive (sent off-list or not yet received): [email protected]

#10 lore raw

Re: [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 01.03.16 at 10:02, <[email protected]> wrote:
xen/common/schedule.c +6 -1
@@ -752,14 +766,20 @@ static int vcpu_set_affinity(
     struct vcpu *v, const cpumask_t *affinity, cpumask_t *which)
 {
     spinlock_t *lock;
+    int ret = 0;
 
     lock = vcpu_schedule_lock_irq(v);
 
-    cpumask_copy(which, affinity);
+    if ( v->affinity_broken )
+        ret = -EBUSY;
+    else
+    {
+        cpumask_copy(which, affinity);
 
Quoted 9 lines

xen/common/schedule.c static int vcpu_set_affinity( · line 781

Quoted 6 lines
xen/common/schedule.c +3 -3
-    /* Always ask the scheduler to re-evaluate placement
-     * when changing the affinity */
-    set_bit(_VPF_migrating, &v->pause_flags);
+        /* Always ask the scheduler to re-evaluate placement
+         * when changing the affinity */
+        set_bit(_VPF_migrating, &v->pause_flags);

When you touch code like this, would it be possible to at once fix the coding style issues it (the comment in this case) has?

Sure, NP.

Quoted 11 lines

xen/common/schedule.c void watchdog_domain_destroy(struct domain *d) · line 1001

Quoted 6 lines
xen/common/schedule.c +1 -0
@@ -978,6 +998,51 @@ void watchdog_domain_destroy(struct domain *d)
         kill_timer(&d->watchdog_timer[i]);
 }
 
+static long do_pin_temp(int cpu)

As expressed before, throughout this patch I dislike the "temp" naming, when the temporary nature of this operation isn't being enforced by anything.

Quoted 6 lines

Apart from that I (vaguely) recall there having been previous suggestions in the direction of (temporary), which have got rejected.

On both points I think we need to have input from the scheduler maintainers.

Okay. I don't mind changing the name. We should just agree on one.

Quoted 8 lines

xen/common/schedule.c void watchdog_domain_destroy(struct domain *d) · line 1005

Quoted 5 lines
xen/common/schedule.c +4 -0
+{
+    struct vcpu *v = current;
+    spinlock_t *lock;
+    long ret = -EINVAL;

"int" seems completely sufficient for both the variable and the function return type.

Hmm, yes.

Quoted 5 lines
Quoted 5 lines
(quoted diff) +0 -0
@@ -1087,6 +1152,23 @@ ret_t do_sched_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         break;
     }
 
Quoted 13 lines

xen/common/schedule.c ret_t do_sched_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 1165

Quoted 11 lines
xen/common/schedule.c +11 -0
+    case SCHEDOP_pin_temp:
+    {
+        struct sched_pin_temp sched_pin_temp;
+
+        ret = -EFAULT;
+        if ( copy_from_guest(&sched_pin_temp, arg, 1) )
+            break;
+
+        ret = -EPERM;
+        if ( !is_hardware_domain(current->domain) )
+            break;

I'd generally suggest swapping these two.

Will do.

Quoted 10 lines
Quoted 10 lines
xen/include/public/sched.h +3 -0
@@ -118,6 +118,17 @@
  * With id != 0 and timeout != 0, poke watchdog timer and set new timeout.
  */
 #define SCHEDOP_watchdog    6
+
+/*
+ * Temporarily pin the current vcpu to one physical cpu or undo that pinning.
Quoted 8 lines

xen/include/public/sched.h line 128

Quoted 5 lines
xen/include/public/sched.h +5 -0
+ * @arg == pointer to sched_pin_temp_t structure.
+ *
+ * Setting pcpu to -1 will undo a previous temporary pinning and restore the
+ * previous cpu affinity. The temporary aspect of the pinning isn't enforced
+ * by the hypervisor.

This comment is now out of sync with the code, since you now accept any negative CPU number as "undo" request.

Will change it.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#11 lore raw

Re: [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

In reply to Juergen Gross
Quoted 60 lines · Juergen Gross

Some hardware (e.g. Dell studio 1555 laptops) require SMIs to be called on physical cpu 0 only. Linux drivers like dcdbas or i8k try to achieve this by pinning the running thread to cpu 0, but in Dom0 this is not enough: the vcpu must be pinned to physical cpu 0 via Xen, too.

Add a stable hypercall option SCHEDOP_pin_temp to the sched_op hypercall to achieve this. It is taking a physical cpu number as parameter. If pinning is possible (the calling domain has the privilege to make the call and the cpu is available in the domain's cpupool) the calling vcpu is pinned to the specified cpu. The old cpu affinity is saved. To undo the temporary pinning a cpu -1 is specified. This will restore the original cpu affinity for the vcpu.

---
V2: - limit operation to hardware domain as suggested by Jan Beulich
- some style issues corrected as requested by Jan Beulich
- use fixed width types in interface as requested by Jan Beulich
- add compat layer checking as requested by Jan Beulich

---
 xen/common/compat/schedule.c |  4 ++
 xen/common/schedule.c        | 92 +++++++++++++++++++++++++++++++++++++++++---
 xen/include/public/sched.h   | 17 ++++++++
 xen/include/xlat.lst         |  1 +
 4 files changed, 109 insertions(+), 5 deletions(-)
xen/common/compat/schedule.c +4 -0
index 812c550..73b0f01 100644
@@ -10,6 +10,10 @@
 
 #define do_sched_op compat_sched_op
 
+#define xen_sched_pin_temp sched_pin_temp
+CHECK_sched_pin_temp;
+#undef xen_sched_pin_temp
+
 #define xen_sched_shutdown sched_shutdown
 CHECK_sched_shutdown;
 #undef xen_sched_shutdown
xen/common/schedule.c +6 -0
index b0d4b18..653f852 100644
@@ -271,6 +271,12 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
     struct scheduler *old_ops;
     void *old_domdata;
 
+    for_each_vcpu ( d, v )
+    {
+        if ( v->affinity_broken )
+            return -EBUSY;
+    }
+
     domdata = SCHED_OP(c->sched, alloc_domdata, d);
     if ( domdata == NULL )
         return -ENOMEM;

xen/common/schedule.c int cpu_disable_scheduler(unsigned int cpu) · line 684

Quoted 10 lines
xen/common/schedule.c +7 -0
@@ -669,6 +675,14 @@ int cpu_disable_scheduler(unsigned int cpu)
             if ( cpumask_empty(&online_affinity) &&
                  cpumask_test_cpu(cpu, v->cpu_hard_affinity) )
             {
+                if ( v->affinity_broken )
+                {
+                    /* The vcpu is temporarily pinned, can't move it. */
+                    vcpu_schedule_unlock_irqrestore(lock, flags, v);
+                    ret = -EBUSY;
+                    break;
+                }

Does this mean that if the user closes the laptop lid while one of these drivers has vcpu0 pinned, that Xen will crash (see xen/arch/x86/smpboot.c:__cpu_disable())? Or is it the OS's job to make sure that all temporary pins are removed before suspending?

Also -- have you actually tested the "cpupool move while pinned" functionality to make sure it actually works? There's a weird bit in cpupool_unassign_cpu_helper() where after calling cpu_disable_scheduler(cpu), it unconditionally sets the cpu bit in the cpupool_free_cpus mask, even if it returns an error. That can't be right, even for the existing -EAGAIN case, can it?

I see that you have a loop to retry this call several times in the next patch; but what if it fails every time -- what state is the system in?

And, in general, what happens if the device driver gets mixed up and forgets to unpin the vcpu? Is the only recourse to reboot your host (or deal with the fact that you can't reconfigure your cpupools)?

-George

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#12 lore raw

Re: [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

Quoted 84 lines · George Dunlap
Quoted 60 lines · Juergen Gross

Some hardware (e.g. Dell studio 1555 laptops) require SMIs to be called on physical cpu 0 only. Linux drivers like dcdbas or i8k try to achieve this by pinning the running thread to cpu 0, but in Dom0 this is not enough: the vcpu must be pinned to physical cpu 0 via Xen, too.

Add a stable hypercall option SCHEDOP_pin_temp to the sched_op hypercall to achieve this. It is taking a physical cpu number as parameter. If pinning is possible (the calling domain has the privilege to make the call and the cpu is available in the domain's cpupool) the calling vcpu is pinned to the specified cpu. The old cpu affinity is saved. To undo the temporary pinning a cpu -1 is specified. This will restore the original cpu affinity for the vcpu.

---
V2: - limit operation to hardware domain as suggested by Jan Beulich
- some style issues corrected as requested by Jan Beulich
- use fixed width types in interface as requested by Jan Beulich
- add compat layer checking as requested by Jan Beulich

---
 xen/common/compat/schedule.c |  4 ++
 xen/common/schedule.c        | 92 +++++++++++++++++++++++++++++++++++++++++---
 xen/include/public/sched.h   | 17 ++++++++
 xen/include/xlat.lst         |  1 +
 4 files changed, 109 insertions(+), 5 deletions(-)
xen/common/compat/schedule.c +4 -0
index 812c550..73b0f01 100644
@@ -10,6 +10,10 @@
 
 #define do_sched_op compat_sched_op
 
+#define xen_sched_pin_temp sched_pin_temp
+CHECK_sched_pin_temp;
+#undef xen_sched_pin_temp
+
 #define xen_sched_shutdown sched_shutdown
 CHECK_sched_shutdown;
 #undef xen_sched_shutdown
xen/common/schedule.c +6 -0
index b0d4b18..653f852 100644
@@ -271,6 +271,12 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
     struct scheduler *old_ops;
     void *old_domdata;
 
+    for_each_vcpu ( d, v )
+    {
+        if ( v->affinity_broken )
+            return -EBUSY;
+    }
+
     domdata = SCHED_OP(c->sched, alloc_domdata, d);
     if ( domdata == NULL )
         return -ENOMEM;

xen/common/schedule.c int cpu_disable_scheduler(unsigned int cpu) · line 684

Quoted 10 lines
xen/common/schedule.c +7 -0
@@ -669,6 +675,14 @@ int cpu_disable_scheduler(unsigned int cpu)
             if ( cpumask_empty(&online_affinity) &&
                  cpumask_test_cpu(cpu, v->cpu_hard_affinity) )
             {
+                if ( v->affinity_broken )
+                {
+                    /* The vcpu is temporarily pinned, can't move it. */
+                    vcpu_schedule_unlock_irqrestore(lock, flags, v);
+                    ret = -EBUSY;
+                    break;
+                }

Does this mean that if the user closes the laptop lid while one of these drivers has vcpu0 pinned, that Xen will crash (see xen/arch/x86/smpboot.c:__cpu_disable())? Or is it the OS's job to make sure that all temporary pins are removed before suspending?

Also -- have you actually tested the "cpupool move while pinned" functionality to make sure it actually works? There's a weird bit in cpupool_unassign_cpu_helper() where after calling cpu_disable_scheduler(cpu), it unconditionally sets the cpu bit in the cpupool_free_cpus mask, even if it returns an error. That can't be right, even for the existing -EAGAIN case, can it?

Quoted 6 lines

I see that you have a loop to retry this call several times in the next patch; but what if it fails every time -- what state is the system in?

And, in general, what happens if the device driver gets mixed up and forgets to unpin the vcpu? Is the only recourse to reboot your host (or deal with the fact that you can't reconfigure your cpupools)?

(I should say, I think this probably is the best solution to this problem; I just want to make sure we think about the error cases carefully.)

-George

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#13 lore raw

Re: [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

Quoted 45 lines
Quoted 1 line
Quoted 1 line · On 01.03.16 at 16:55, <[email protected]> wrote:
Quoted 43 lines · George Dunlap
Quoted 16 lines · Juergen Gross
xen/common/schedule.c +6 -0
@@ -271,6 +271,12 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
     struct scheduler *old_ops;
     void *old_domdata;
 
+    for_each_vcpu ( d, v )
+    {
+        if ( v->affinity_broken )
+            return -EBUSY;
+    }
+
     domdata = SCHED_OP(c->sched, alloc_domdata, d);
     if ( domdata == NULL )
         return -ENOMEM;

xen/common/schedule.c int cpu_disable_scheduler(unsigned int cpu) · line 684

Quoted 10 lines
xen/common/schedule.c +7 -0
@@ -669,6 +675,14 @@ int cpu_disable_scheduler(unsigned int cpu)
             if ( cpumask_empty(&online_affinity) &&
                  cpumask_test_cpu(cpu, v->cpu_hard_affinity) )
             {
+                if ( v->affinity_broken )
+                {
+                    /* The vcpu is temporarily pinned, can't move it. */
+                    vcpu_schedule_unlock_irqrestore(lock, flags, v);
+                    ret = -EBUSY;
+                    break;
+                }

Does this mean that if the user closes the laptop lid while one of these drivers has vcpu0 pinned, that Xen will crash (see xen/arch/x86/smpboot.c:__cpu_disable())? Or is it the OS's job to make sure that all temporary pins are removed before suspending?

Also -- have you actually tested the "cpupool move while pinned" functionality to make sure it actually works? There's a weird bit in cpupool_unassign_cpu_helper() where after calling cpu_disable_scheduler(cpu), it unconditionally sets the cpu bit in the cpupool_free_cpus mask, even if it returns an error. That can't be right, even for the existing -EAGAIN case, can it?

I see that you have a loop to retry this call several times in the next patch; but what if it fails every time -- what state is the system in?

Quoted 6 lines
Quoted 3 lines

And, in general, what happens if the device driver gets mixed up and forgets to unpin the vcpu? Is the only recourse to reboot your host (or deal with the fact that you can't reconfigure your cpupools)?

(I should say, I think this probably is the best solution to this problem; I just want to make sure we think about the error cases carefully.)

I guess in the worst case there could be a utility or xl command doing the missing unpin in such an emergency?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#14 lore raw

Re: [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

In reply to George Dunlap
Quoted 68 lines · George Dunlap
Quoted 67 lines · Juergen Gross

Some hardware (e.g. Dell studio 1555 laptops) require SMIs to be called on physical cpu 0 only. Linux drivers like dcdbas or i8k try to achieve this by pinning the running thread to cpu 0, but in Dom0 this is not enough: the vcpu must be pinned to physical cpu 0 via Xen, too.

Add a stable hypercall option SCHEDOP_pin_temp to the sched_op hypercall to achieve this. It is taking a physical cpu number as parameter. If pinning is possible (the calling domain has the privilege to make the call and the cpu is available in the domain's cpupool) the calling vcpu is pinned to the specified cpu. The old cpu affinity is saved. To undo the temporary pinning a cpu -1 is specified. This will restore the original cpu affinity for the vcpu.

---
V2: - limit operation to hardware domain as suggested by Jan Beulich
- some style issues corrected as requested by Jan Beulich
- use fixed width types in interface as requested by Jan Beulich
- add compat layer checking as requested by Jan Beulich

---
 xen/common/compat/schedule.c |  4 ++
 xen/common/schedule.c        | 92 +++++++++++++++++++++++++++++++++++++++++---
 xen/include/public/sched.h   | 17 ++++++++
 xen/include/xlat.lst         |  1 +
 4 files changed, 109 insertions(+), 5 deletions(-)
xen/common/compat/schedule.c +4 -0
index 812c550..73b0f01 100644
@@ -10,6 +10,10 @@
 
 #define do_sched_op compat_sched_op
 
+#define xen_sched_pin_temp sched_pin_temp
+CHECK_sched_pin_temp;
+#undef xen_sched_pin_temp
+
 #define xen_sched_shutdown sched_shutdown
 CHECK_sched_shutdown;
 #undef xen_sched_shutdown
xen/common/schedule.c +10 -0
index b0d4b18..653f852 100644
@@ -271,6 +271,12 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
     struct scheduler *old_ops;
     void *old_domdata;
 
+    for_each_vcpu ( d, v )
+    {
+        if ( v->affinity_broken )
+            return -EBUSY;
+    }
+
     domdata = SCHED_OP(c->sched, alloc_domdata, d);
     if ( domdata == NULL )
         return -ENOMEM;
@@ -669,6 +675,14 @@ int cpu_disable_scheduler(unsigned int cpu)
             if ( cpumask_empty(&online_affinity) &&
                  cpumask_test_cpu(cpu, v->cpu_hard_affinity) )
             {
+                if ( v->affinity_broken )
+                {
+                    /* The vcpu is temporarily pinned, can't move it. */
+                    vcpu_schedule_unlock_irqrestore(lock, flags, v);
Quoted 8 lines

xen/common/schedule.c int cpu_disable_scheduler(unsigned int cpu) · line 684

Quoted 3 lines
xen/common/schedule.c +3 -0
+                    ret = -EBUSY;
+                    break;
+                }

Does this mean that if the user closes the laptop lid while one of these drivers has vcpu0 pinned, that Xen will crash (see xen/arch/x86/smpboot.c:__cpu_disable())? Or is it the OS's job to make sure that all temporary pins are removed before suspending?

Yes, this must be ensured by the OS.

Quoted 6 lines

Also -- have you actually tested the "cpupool move while pinned" functionality to make sure it actually works? There's a weird bit in cpupool_unassign_cpu_helper() where after calling cpu_disable_scheduler(cpu), it unconditionally sets the cpu bit in the cpupool_free_cpus mask, even if it returns an error. That can't be right, even for the existing -EAGAIN case, can it?

That should be no problem. Such a failure can be repaired easily by adding the cpu to the cpupool again. Adding a comment seems to be a good idea. :-)

What is wrong and even worse, schedule_cpu_switch() returning an error will leak domlist_read_lock. I'll write another patch to correct this issue.

Quoted 2 lines

I see that you have a loop to retry this call several times in the next patch; but what if it fails every time -- what state is the system in?

The cpu can be added to the original cpupool via "xl cpupool-add" again.

Quoted 3 lines

And, in general, what happens if the device driver gets mixed up and forgets to unpin the vcpu? Is the only recourse to reboot your host (or deal with the fact that you can't reconfigure your cpupools)?

Unless we add a "forced" option to "xl vcpu-pin", yes.

Thanks for the thorough review,

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#15 lore raw

Re: [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

Quoted 11 lines · Juergen Gross
Quoted 10 lines · George Dunlap

Also -- have you actually tested the "cpupool move while pinned" functionality to make sure it actually works?  There's a weird bit in
cpupool_unassign_cpu_helper() where after calling cpu_disable_scheduler(cpu), it unconditionally sets the cpu bit in the
cpupool_free_cpus mask, even if it returns an error.  That can't be right, even for the existing -EAGAIN case, can it?

Quoted 3 lines

That should be no problem. Such a failure can be repaired easily by adding the cpu to the cpupool again.

And there's not much else one can do, I would say. When we are in cpu_disable_scheduler(), coming from cpupool_unassign_cpu()-->cpupool_unassign_cpu() we're already halfway through removing the cpu from the pool (e.g., we already cleared the relevant bit from the cpupool's cpu_valid mask).

And we don't actually want to revert that, as doing so would allow the scheduler to start again moving vcpus to that cpu (and the following attempts will risk failing with EAGAIN again :-D).

FWIW, I've also found that part rather weird for quite some time... But it does indeed makes sense, IMO.

Quoted 3 lines

Adding a comment seems to be a
good idea. :-)

Yep. Should we also add an error message for the user to be able to see it, even if she can't read the comment in the source code? (Not necessarily right there, if that would make it trigger too much... just in a place where it can be seen in the case the user actually need to do something).

Quoted 4 lines

What is wrong and even worse, schedule_cpu_switch() returning an error
will leak domlist_read_lock.

Indeed, good catch. :-)

Quoted 7 lines
Quoted 5 lines

And, in general, what happens if the device driver gets mixed up and
forgets to unpin the vcpu?  Is the only recourse to reboot your host (or
deal with the fact that you can't reconfigure your cpupools)?

Unless we add a "forced" option to "xl vcpu-pin", yes.

Which would be fine to have, IMO. I'm not sure if it would better be an `xl vcpu-pin' flag, or a separate utility (as Jan is also saying).

A separate utility would fit better the "emergency nature" of the thing, avoiding having to clobber xl for that (as this will be the only, pretty uncommon, case where such flag would be needed).

However, an xl flag is easier to add, easier to document and easier and more natural to find, from the point of view of an user that really needs it. And perhaps it could turn out useful for other situations in future. So, I guess I'd say:
 - yes, let's add that
 - let's do it as a "force flag" of `xl vcpu-pin'.

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#16 lore raw

Re: [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

Quoted 21 lines · Dario Faggioli
Quoted 11 lines · Juergen Gross
Quoted 10 lines · George Dunlap

Also -- have you actually tested the "cpupool move while pinned" functionality to make sure it actually works? There's a weird bit in
cpupool_unassign_cpu_helper() where after calling cpu_disable_scheduler(cpu), it unconditionally sets the cpu bit in the
cpupool_free_cpus mask, even if it returns an error. That can't be right, even for the existing -EAGAIN case, can it?

Quoted 3 lines

That should be no problem. Such a failure can be repaired easily by adding the cpu to the cpupool again.

And there's not much else one can do, I would say. When we are in cpu_disable_scheduler(), coming from cpupool_unassign_cpu()-->cpupool_unassign_cpu() we're already halfway through removing the cpu from the pool (e.g., we already cleared the relevant bit from the cpupool's cpu_valid mask).

Quoted 3 lines

And we don't actually want to revert that, as doing so would allow the scheduler to start again moving vcpus to that cpu (and the following attempts will risk failing with EAGAIN again :-D).

Yep.

Quoted 4 lines

FWIW, I've also found that part rather weird for quite some time... But it does indeed makes sense, IMO.

Quoted 8 lines
Quoted 3 lines

Adding a comment seems to be a
good idea. :-)

Yep. Should we also add an error message for the user to be able to see it, even if she can't read the comment in the source code? (Not necessarily right there, if that would make it trigger too much... just in a place where it can be seen in the case the user actually need to do something).

I'd rather add the error message to xl. That's where the user will see it and where he can react at once. The message can even tell the user the correct command, which would be a very strange thing to do in the hypervisor.

Another patch, I guess. :-)

Quoted 21 lines
Quoted 5 lines

What is wrong and even worse, schedule_cpu_switch() returning an error
will leak domlist_read_lock.

Indeed, good catch. :-)

Quoted 7 lines
Quoted 5 lines

And, in general, what happens if the device driver gets mixed up and
forgets to unpin the vcpu? Is the only recourse to reboot your host (or
deal with the fact that you can't reconfigure your cpupools)?

Unless we add a "forced" option to "xl vcpu-pin", yes.

Which would be fine to have, IMO. I'm not sure if it would better be an `xl vcpu-pin' flag, or a separate utility (as Jan is also saying).

A separate utility would fit better the "emergency nature" of the thing, avoiding having to clobber xl for that (as this will be the only, pretty uncommon, case where such flag would be needed).

Quoted 6 lines

However, an xl flag is easier to add, easier to document and easier and more natural to find, from the point of view of an user that really needs it. And perhaps it could turn out useful for other situations in future. So, I guess I'd say:
- yes, let's add that
- let's do it as a "force flag" of `xl vcpu-pin'.

Okay, patch will follow...

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#17 lore raw

Re: [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

Quoted 10 lines · Juergen Gross
Quoted 9 lines · Dario Faggioli


Yep. Should we also add an error message for the user to be able to see
it, even if she can't read the comment in the source code? (Not necessarily right there, if that would make it trigger too much... just
in a place where it can be seen in the case the user actually need to
do something).

Quoted 6 lines

I'd rather add the error message to xl. That's where the user will see
it and where he can react at once. The message can even tell the user the correct command, which would be a very strange thing to do in the hypervisor.

Sure, wherever it's most useful.

Quoted 2 lines

Another patch, I guess. :-)

Yeah, sorry. :-)

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#18 lore raw

Re: [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

Quoted 17 lines · Dario Faggioli
Quoted 10 lines · Juergen Gross
Quoted 9 lines · Dario Faggioli

Yep. Should we also add an error message for the user to be able to see
it, even if she can't read the comment in the source code? (Not necessarily right there, if that would make it trigger too much... just
in a place where it can be seen in the case the user actually need to
do something).

Quoted 6 lines

I'd rather add the error message to xl. That's where the user will see
it and where he can react at once. The message can even tell the user the correct command, which would be a very strange thing to do in the hypervisor.

Quoted 5 lines

Sure, wherever it's most useful.

Quoted 2 lines

Another patch, I guess. :-)

Yeah, sorry. :-)

Sarcio ergo sum! :-)

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#19 lore raw

Re: [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

In reply to Dario Faggioli
Quoted 57 lines · Dario Faggioli
Quoted 11 lines · Juergen Gross
Quoted 10 lines · George Dunlap

Also -- have you actually tested the "cpupool move while pinned" functionality to make sure it actually works? There's a weird bit in
cpupool_unassign_cpu_helper() where after calling cpu_disable_scheduler(cpu), it unconditionally sets the cpu bit in the
cpupool_free_cpus mask, even if it returns an error. That can't be right, even for the existing -EAGAIN case, can it?

Quoted 3 lines

That should be no problem. Such a failure can be repaired easily by adding the cpu to the cpupool again.

And there's not much else one can do, I would say. When we are in cpu_disable_scheduler(), coming from cpupool_unassign_cpu()-->cpupool_unassign_cpu() we're already halfway through removing the cpu from the pool (e.g., we already cleared the relevant bit from the cpupool's cpu_valid mask).

And we don't actually want to revert that, as doing so would allow the scheduler to start again moving vcpus to that cpu (and the following attempts will risk failing with EAGAIN again :-D).

FWIW, I've also found that part rather weird for quite some time... But it does indeed makes sense, IMO.

Quoted 3 lines

Adding a comment seems to be a
good idea. :-)

Yep. Should we also add an error message for the user to be able to see it, even if she can't read the comment in the source code? (Not necessarily right there, if that would make it trigger too much... just in a place where it can be seen in the case the user actually need to do something).

Quoted 4 lines

What is wrong and even worse, schedule_cpu_switch() returning an error
will leak domlist_read_lock.

Indeed, good catch. :-)

Quoted 7 lines
Quoted 5 lines

And, in general, what happens if the device driver gets mixed up and
forgets to unpin the vcpu? Is the only recourse to reboot your host (or
deal with the fact that you can't reconfigure your cpupools)?

Unless we add a "forced" option to "xl vcpu-pin", yes.

Which would be fine to have, IMO. I'm not sure if it would better be an `xl vcpu-pin' flag, or a separate utility (as Jan is also saying).

A separate utility would fit better the "emergency nature" of the thing, avoiding having to clobber xl for that (as this will be the only, pretty uncommon, case where such flag would be needed).

Quoted 6 lines

However, an xl flag is easier to add, easier to document and easier and more natural to find, from the point of view of an user that really needs it. And perhaps it could turn out useful for other situations in future. So, I guess I'd say:
- yes, let's add that
- let's do it as a "force flag" of `xl vcpu-pin'.

Which raises the question: how to do that on the libxl level?

a) expand libxl_set_vcpuaffinity() with another parameter (is this even possible? I could do some ifdeffery, but the API would change...)

b) add a libxl_set_vcpuaffinity_force() variant

c) imply the force flag by specifying both hard and soft maps as NULL (it _is_ basically just that: keep both affinity sets), implying that it makes no sense to specify any affinities with the -f flag (which renders the "force" meaning rather strange, would be more a "restore" now).

Juergen

Quoted 4 lines

Regards,
Dario

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#20 lore raw

Re: [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

Quoted 10 lines · Juergen Gross
Quoted 9 lines · Dario Faggioli


However, an xl flag is easier to add, easier to document and easier and
more natural to find, from the point of view of an user that really needs it. And perhaps it could turn out useful for other situations in
future. So, I guess I'd say:
 - yes, let's add that
 - let's do it as a "force flag" of `xl vcpu-pin'.

Quoted 2 lines

Which raises the question: how to do that on the libxl level?

Ah, right.

Quoted 6 lines

a) expand libxl_set_vcpuaffinity() with another parameter (is this even
   possible? I could do some ifdeffery, but the API would change...)

b) add a libxl_set_vcpuaffinity_force() variant

Quoted 9 lines

c) imply the force flag by specifying both hard and soft maps as NULL (it _is_ basically just that: keep both affinity sets), implying that
   it makes no sense to specify any affinities with the -f flag (which
   renders the "force" meaning rather strange, would be more a "restore"
   now).

Eheh, tools' maintainers' call. My preference would be b).

I don't like a), mostly because that would mean everyone will need to specify a parameter that it is really only necessary in special cases.

I could live with c), but it indeed makes the semantic too convoluted for my taste.

I guess, however, that even if going for b), we need to decide whether to require a cpumask or not, and what to do if one passes NULL. Maybe we can have a cpumask parameter and,
 - if it is not NULL, force affinity to that,
 - if it is NULL, just 'restore';
what do you think?

Actually, at Xen level, the override only acts on hard affinity... should libxl take only one cpumask (for hard affinity only), or both hard and soft?
I'd say just one for hard is enough, unless we want to make space for a potential future situation where we will want to break and restore soft affinity as well...

Dario
--
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#21 lore raw

Re: [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

Quoted 38 lines · Dario Faggioli
Quoted 10 lines · Juergen Gross
Quoted 9 lines · Dario Faggioli

However, an xl flag is easier to add, easier to document and easier and
more natural to find, from the point of view of an user that really needs it. And perhaps it could turn out useful for other situations in
future. So, I guess I'd say:
- yes, let's add that
- let's do it as a "force flag" of `xl vcpu-pin'.

Quoted 2 lines

Which raises the question: how to do that on the libxl level?

Ah, right.

Quoted 6 lines

a) expand libxl_set_vcpuaffinity() with another parameter (is this even
possible? I could do some ifdeffery, but the API would change...)

b) add a libxl_set_vcpuaffinity_force() variant

Quoted 9 lines

c) imply the force flag by specifying both hard and soft maps as NULL (it _is_ basically just that: keep both affinity sets), implying
that
it makes no sense to specify any affinities with the -f flag
(which
renders the "force" meaning rather strange, would be more a
"restore"
now).

Eheh, tools' maintainers' call. My preference would be b).

I don't like a), mostly because that would mean everyone will need to specify a parameter that it is really only necessary in special cases.

I could live with c), but it indeed makes the semantic too convoluted for my taste.

Quoted 6 lines

I guess, however, that even if going for b), we need to decide whether to require a cpumask or not, and what to do if one passes NULL. Maybe we can have a cpumask parameter and,
- if it is not NULL, force affinity to that,
- if it is NULL, just 'restore';
what do you think?

I would just let the force flag restore the old setting (thus clearing the affinity_broken flag) and then apply the normal affinity settings.

Quoted 3 lines

Actually, at Xen level, the override only acts on hard affinity... should libxl take only one cpumask (for hard affinity only), or both hard and soft?

Just as the user is specifying: 0, 1 or 2.

Quoted 3 lines

I'd say just one for hard is enough, unless we want to make space for a potential future situation where we will want to break and restore soft affinity as well...

The force flag would be just an add-on. That's rather easy in the hypervisor and in the tools.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#22 lore raw

Re: [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

In reply to George Dunlap

Hi,

-----Original Message-----
From: Xen-devel [mailto:[email protected]] On Behalf Of George Dunlap
Sent: 01 March 2016 15:53
To: Juergen Gross <[email protected]>; [email protected]
Cc: Wei Liu <[email protected]>; Stefano Stabellini <[email protected]>; George Dunlap <[email protected]>; Andrew Cooper <[email protected]>; Dario Faggioli <[email protected]>; Ian Jackson <[email protected]>; David Vrabel <[email protected]>; [email protected]
Subject: Re: [Xen-devel] [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

Quoted 56 lines · Juergen Gross

Some hardware (e.g. Dell studio 1555 laptops) require SMIs to be called on physical cpu 0 only. Linux drivers like dcdbas or i8k try to achieve this by pinning the running thread to cpu 0, but in Dom0 this is not enough: the vcpu must be pinned to physical cpu 0 via Xen, too.

Add a stable hypercall option SCHEDOP_pin_temp to the sched_op hypercall to achieve this. It is taking a physical cpu number as parameter. If pinning is possible (the calling domain has the privilege to make the call and the cpu is available in the domain's cpupool) the calling vcpu is pinned to the specified cpu. The old cpu affinity is saved. To undo the temporary pinning a cpu -1 is specified. This will restore the original cpu affinity for the vcpu.

---
V2: - limit operation to hardware domain as suggested by Jan Beulich
- some style issues corrected as requested by Jan Beulich
- use fixed width types in interface as requested by Jan Beulich
- add compat layer checking as requested by Jan Beulich

---
 xen/common/compat/schedule.c |  4 ++
 xen/common/schedule.c        | 92 +++++++++++++++++++++++++++++++++++++++++---
 xen/include/public/sched.h   | 17 ++++++++
 xen/include/xlat.lst         |  1 +
 4 files changed, 109 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/xen/common/compat/schedule.c index 812c550..73b0f01 100644

xen/common/compat/schedule.c +3 -0
@@ -10,6 +10,10 @@
 
 #define do_sched_op compat_sched_op
 
+#define xen_sched_pin_temp sched_pin_temp CHECK_sched_pin_temp; 
+#undef xen_sched_pin_temp
+
 #define xen_sched_shutdown sched_shutdown  CHECK_sched_shutdown;  
 #undef xen_sched_shutdown diff --git a/xen/common/schedule.c 

b/xen/common/schedule.c index b0d4b18..653f852 100644

xen/common/schedule.c +6 -0
@@ -271,6 +271,12 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
     struct scheduler *old_ops;
     void *old_domdata;
 
+    for_each_vcpu ( d, v )
+    {
+        if ( v->affinity_broken )
+            return -EBUSY;
+    }
+
     domdata = SCHED_OP(c->sched, alloc_domdata, d);
     if ( domdata == NULL )
         return -ENOMEM;

xen/common/schedule.c int cpu_disable_scheduler(unsigned int cpu) · line 684

Quoted 10 lines
xen/common/schedule.c +7 -0
@@ -669,6 +675,14 @@ int cpu_disable_scheduler(unsigned int cpu)
             if ( cpumask_empty(&online_affinity) &&
                  cpumask_test_cpu(cpu, v->cpu_hard_affinity) )
             {
+                if ( v->affinity_broken )
+                {
+                    /* The vcpu is temporarily pinned, can't move it. */
+                    vcpu_schedule_unlock_irqrestore(lock, flags, v);
+                    ret = -EBUSY;
+                    break;
+                }

Does this mean that if the user closes the laptop lid while one of these drivers has vcpu0 pinned, that Xen will crash (see xen/arch/x86/smpboot.c:__cpu_disable())? Or is it the OS's job to make sure that all temporary pins are removed before suspending?

Also -- have you actually tested the "cpupool move while pinned"
functionality to make sure it actually works? There's a weird bit in
cpupool_unassign_cpu_helper() where after calling cpu_disable_scheduler(cpu), it unconditionally sets the cpu bit in the cpupool_free_cpus mask, even if it returns an error. That can't be right, even for the existing -EAGAIN case, can it?

I see that you have a loop to retry this call several times in the next patch; but what if it fails every time -- what state is the system in?

And, in general, what happens if the device driver gets mixed up and forgets to unpin the vcpu? Is the only recourse to reboot your host (or deal with the fact that you can't reconfigure your cpupools)?

-George

Sorry, lost the original thread so replying at the top of mail chain.

+static XSM_INLINE int xsm_schedop_pin_temp(XSM_DEFAULT_VOID) +{
+ XSM_ASSERT_ACTION(XSM_PRIV);
+ return xsm_default_action(action, current->domain, NULL); +}

Is the intention is to restrict the hypercall usage to dom0 only ?

Anshul Makkar

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#23 lore raw

Re: [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

Quoted 98 lines · Anshul Makkar

Hi,

-----Original Message-----
From: Xen-devel [mailto:[email protected]] On Behalf Of George Dunlap
Sent: 01 March 2016 15:53
To: Juergen Gross <[email protected]>; [email protected]
Cc: Wei Liu <[email protected]>; Stefano Stabellini <[email protected]>; George Dunlap <[email protected]>; Andrew Cooper <[email protected]>; Dario Faggioli <[email protected]>; Ian Jackson <[email protected]>; David Vrabel <[email protected]>; [email protected]
Subject: Re: [Xen-devel] [PATCH v2 2/3] xen: add hypercall option to temporarily pin a vcpu

Quoted 56 lines · Juergen Gross

Some hardware (e.g. Dell studio 1555 laptops) require SMIs to be called on physical cpu 0 only. Linux drivers like dcdbas or i8k try to achieve this by pinning the running thread to cpu 0, but in Dom0 this is not enough: the vcpu must be pinned to physical cpu 0 via Xen, too.

Add a stable hypercall option SCHEDOP_pin_temp to the sched_op hypercall to achieve this. It is taking a physical cpu number as parameter. If pinning is possible (the calling domain has the privilege to make the call and the cpu is available in the domain's cpupool) the calling vcpu is pinned to the specified cpu. The old cpu affinity is saved. To undo the temporary pinning a cpu -1 is specified. This will restore the original cpu affinity for the vcpu.

---
V2: - limit operation to hardware domain as suggested by Jan Beulich
- some style issues corrected as requested by Jan Beulich
- use fixed width types in interface as requested by Jan Beulich
- add compat layer checking as requested by Jan Beulich

---
 xen/common/compat/schedule.c |  4 ++
 xen/common/schedule.c        | 92 +++++++++++++++++++++++++++++++++++++++++---
 xen/include/public/sched.h   | 17 ++++++++
 xen/include/xlat.lst         |  1 +
 4 files changed, 109 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/xen/common/compat/schedule.c index 812c550..73b0f01 100644

xen/common/compat/schedule.c +3 -0
@@ -10,6 +10,10 @@
 
 #define do_sched_op compat_sched_op
 
+#define xen_sched_pin_temp sched_pin_temp CHECK_sched_pin_temp; 
+#undef xen_sched_pin_temp
+
 #define xen_sched_shutdown sched_shutdown  CHECK_sched_shutdown;  
 #undef xen_sched_shutdown diff --git a/xen/common/schedule.c 

b/xen/common/schedule.c index b0d4b18..653f852 100644

xen/common/schedule.c +6 -0
@@ -271,6 +271,12 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
     struct scheduler *old_ops;
     void *old_domdata;
 
+    for_each_vcpu ( d, v )
+    {
+        if ( v->affinity_broken )
+            return -EBUSY;
+    }
+
     domdata = SCHED_OP(c->sched, alloc_domdata, d);
     if ( domdata == NULL )
         return -ENOMEM;

xen/common/schedule.c int cpu_disable_scheduler(unsigned int cpu) · line 684

Quoted 10 lines
xen/common/schedule.c +7 -0
@@ -669,6 +675,14 @@ int cpu_disable_scheduler(unsigned int cpu)
             if ( cpumask_empty(&online_affinity) &&
                  cpumask_test_cpu(cpu, v->cpu_hard_affinity) )
             {
+                if ( v->affinity_broken )
+                {
+                    /* The vcpu is temporarily pinned, can't move it. */
+                    vcpu_schedule_unlock_irqrestore(lock, flags, v);
+                    ret = -EBUSY;
+                    break;
+                }

Does this mean that if the user closes the laptop lid while one of these drivers has vcpu0 pinned, that Xen will crash (see xen/arch/x86/smpboot.c:__cpu_disable())? Or is it the OS's job to make sure that all temporary pins are removed before suspending?

Also -- have you actually tested the "cpupool move while pinned"
functionality to make sure it actually works? There's a weird bit in
cpupool_unassign_cpu_helper() where after calling cpu_disable_scheduler(cpu), it unconditionally sets the cpu bit in the cpupool_free_cpus mask, even if it returns an error. That can't be right, even for the existing -EAGAIN case, can it?

I see that you have a loop to retry this call several times in the next patch; but what if it fails every time -- what state is the system in?

And, in general, what happens if the device driver gets mixed up and forgets to unpin the vcpu? Is the only recourse to reboot your host (or deal with the fact that you can't reconfigure your cpupools)?

-George

Sorry, lost the original thread so replying at the top of mail chain.

(quoted diff) +5 -0
+static XSM_INLINE int xsm_schedop_pin_temp(XSM_DEFAULT_VOID) 
+{ 
+ XSM_ASSERT_ACTION(XSM_PRIV); 
+ return xsm_default_action(action, current->domain, NULL); 
+}
Quoted 1 line

Is the intention is to restrict the hypercall usage to dom0 only ?

To be more precise: to the hardware domain (the patch sniplet you are referencing was part of V1 of the series, it isn't existing in V2 any longer).

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#24 lore raw

PATCH v2 3/3 libxc: do some retries in xc_cpupool_removecpu() for EBUSY case

In reply to Juergen Gross

The hypervisor might return EBUSY when trying to remove a cpu from a cpupool when a domain running in this cpupool has pinned a vcpu temporarily. Do some retries in this case, perhaps the situation cleans up.

---
 tools/libxc/xc_cpupool.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
tools/libxc/xc_cpupool.c +12 -1
index c42273e..9f2f95c 100644
@@ -20,8 +20,11 @@
  */
 
 #include <stdarg.h>
+#include <unistd.h>
 #include "xc_private.h"
 
+#define LIBXC_BUSY_RETRIES 5
+
 static int do_sysctl_save(xc_interface *xch, struct xen_sysctl *sysctl)
 {
     int ret;
@@ -141,13 +144,21 @@ int xc_cpupool_removecpu(xc_interface *xch,
                          uint32_t poolid,
                          int cpu)
 {
+    unsigned retries;
+    int err;
     DECLARE_SYSCTL;
 
     sysctl.cmd = XEN_SYSCTL_cpupool_op;
     sysctl.u.cpupool_op.op = XEN_SYSCTL_CPUPOOL_OP_RMCPU;
     sysctl.u.cpupool_op.cpupool_id = poolid;
     sysctl.u.cpupool_op.cpu = (cpu < 0) ? XEN_SYSCTL_CPUPOOL_PAR_ANY : cpu;
-    return do_sysctl_save(xch, &sysctl);
+    for (retries = 0; retries < LIBXC_BUSY_RETRIES; retries++) {
+        err = do_sysctl_save(xch, &sysctl);
+        if (err >= 0 || errno != EBUSY)
+            break;
+        sleep(1);
+    }
+    return err;
 }
 
 int xc_cpupool_movedomain(xc_interface *xch,
Signature · 7 lines
2.6.2


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#25 lore raw

Re: [PATCH v2 3/3] libxc: do some retries in xc_cpupool_removecpu() for EBUSY case

Quoted 30 lines · Juergen Gross

The hypervisor might return EBUSY when trying to remove a cpu from a cpupool when a domain running in this cpupool has pinned a vcpu temporarily. Do some retries in this case, perhaps the situation cleans up.

---
 tools/libxc/xc_cpupool.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
tools/libxc/xc_cpupool.c +3 -0
index c42273e..9f2f95c 100644
@@ -20,8 +20,11 @@
  */
 
 #include <stdarg.h>
+#include <unistd.h>
 #include "xc_private.h"
 
+#define LIBXC_BUSY_RETRIES 5
+
 static int do_sysctl_save(xc_interface *xch, struct xen_sysctl *sysctl)
 {
     int ret;
@@ -141,13 +144,21 @@ int xc_cpupool_removecpu(xc_interface *xch,
                          uint32_t poolid,
                          int cpu)
 {

tools/libxc/xc_cpupool.c int xc_cpupool_removecpu(xc_interface *xch, · line 155

Quoted 10 lines
tools/libxc/xc_cpupool.c +3 -1
+    unsigned retries;
+    int err;
     DECLARE_SYSCTL;
 
     sysctl.cmd = XEN_SYSCTL_cpupool_op;
     sysctl.u.cpupool_op.op = XEN_SYSCTL_CPUPOOL_OP_RMCPU;
     sysctl.u.cpupool_op.cpupool_id = poolid;
     sysctl.u.cpupool_op.cpu = (cpu < 0) ? XEN_SYSCTL_CPUPOOL_PAR_ANY : cpu;
-    return do_sysctl_save(xch, &sysctl);
+    for (retries = 0; retries < LIBXC_BUSY_RETRIES; retries++) {

Libxc coding style requires spaces between ().

tools/libxc/xc_cpupool.c int xc_cpupool_removecpu(xc_interface *xch, · line 157

Quoted 2 lines
tools/libxc/xc_cpupool.c +2 -0
+        err = do_sysctl_save(xch, &sysctl);
+        if (err >= 0 || errno != EBUSY)

Ditto.

Quoted 10 lines
tools/libxc/xc_cpupool.c +4 -0
+            break;
+        sleep(1);
+    }
+    return err;
 }
 
 int xc_cpupool_movedomain(xc_interface *xch,
Signature · 1 line
2.6.2

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#26 lore raw

Re: [PATCH v2 3/3] libxc: do some retries in xc_cpupool_removecpu() for EBUSY case

Quoted 35 lines · Wei Liu
Quoted 34 lines · Juergen Gross

The hypervisor might return EBUSY when trying to remove a cpu from a cpupool when a domain running in this cpupool has pinned a vcpu temporarily. Do some retries in this case, perhaps the situation cleans up.

---
 tools/libxc/xc_cpupool.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
tools/libxc/xc_cpupool.c +5 -0
index c42273e..9f2f95c 100644
@@ -20,8 +20,11 @@
  */
 
 #include <stdarg.h>
+#include <unistd.h>
 #include "xc_private.h"
 
+#define LIBXC_BUSY_RETRIES 5
+
 static int do_sysctl_save(xc_interface *xch, struct xen_sysctl *sysctl)
 {
     int ret;
@@ -141,13 +144,21 @@ int xc_cpupool_removecpu(xc_interface *xch,
                          uint32_t poolid,
                          int cpu)
 {
+    unsigned retries;
+    int err;
     DECLARE_SYSCTL;
 
Quoted 8 lines

tools/libxc/xc_cpupool.c int xc_cpupool_removecpu(xc_interface *xch, · line 155

Quoted 6 lines
tools/libxc/xc_cpupool.c +1 -1
     sysctl.cmd = XEN_SYSCTL_cpupool_op;
     sysctl.u.cpupool_op.op = XEN_SYSCTL_CPUPOOL_OP_RMCPU;
     sysctl.u.cpupool_op.cpupool_id = poolid;
     sysctl.u.cpupool_op.cpu = (cpu < 0) ? XEN_SYSCTL_CPUPOOL_PAR_ANY : cpu;
-    return do_sysctl_save(xch, &sysctl);
+    for (retries = 0; retries < LIBXC_BUSY_RETRIES; retries++) {

Libxc coding style requires spaces between ().

Oops, sorry.

Quoted 13 lines

tools/libxc/xc_cpupool.c int xc_cpupool_removecpu(xc_interface *xch, · line 157

Quoted 3 lines
tools/libxc/xc_cpupool.c +2 -0
+        err = do_sysctl_save(xch, &sysctl);
+        if (err >= 0 || errno != EBUSY)

Ditto.

Quoted 7 lines
tools/libxc/xc_cpupool.c +4 -0
+            break;
+        sleep(1);
+    }
+    return err;
 }
 
 int xc_cpupool_movedomain(xc_interface *xch,

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel