Skip to contentSkip to conversation

xen: Fix BUFIOREQ evtchn init for a stubdom.

PATCH Superseded Anthony PERARD 4 participants 10 messages Jun 26, 2012 – Jun 29, 2012

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xen: Fix BUFIOREQ evtchn init for a stubdom.

This is a missing part from the previous patch that add the BUFIOREQ_EVTCHN parameter. This patch changes the ownership of the buifioreq event channel to the stubdom (when HVM_PARAM_DM_DOMAIN is set within the stubdom).

This patch introduces an helper to replace a xen port.

This fix the initialization of QEMU inside the stubdomain.

---
Change:
- an helper
- the replacement of the buferioreq evtchn is inside iorp->lock now.

 xen/arch/x86/hvm/hvm.c |   37 ++++++++++++++++++++++++++++---------
 1 files changed, 28 insertions(+), 9 deletions(-)
xen/arch/x86/hvm/hvm.c +28 -9
index e0d495d..e8e86c0 100644
@@ -3664,6 +3664,21 @@ static int hvmop_flush_tlb_all(void)
     return 0;
 }
 
+static int hvm_replace_event_channel(struct vcpu *v, uint64_t remote_domid,
+                                     int *port)
+{
+    int old_port, new_port;
+
+    new_port = alloc_unbound_xen_event_channel(v, remote_domid, NULL);
+    if ( new_port < 0 )
+        return new_port;
+
+    /* xchg() ensures that only we free_xen_event_channel() */
+    old_port = xchg(port, new_port);
+    free_xen_event_channel(v, old_port);
+    return 0;
+}
+
 long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE(void) arg)
 
 {
@@ -3777,17 +3792,21 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE(void) arg)
                 iorp = &d->arch.hvm_domain.ioreq;
                 for_each_vcpu ( d, v )
                 {
-                    int old_port, new_port;
-                    new_port = alloc_unbound_xen_event_channel(
-                        v, a.value, NULL);
-                    if ( new_port < 0 )
-                    {
-                        rc = new_port;
+                    rc = hvm_replace_event_channel(v, a.value,
+                                                   &v->arch.hvm_vcpu.xen_port);
+                    if ( rc )
                         break;
+
+                    if ( v->vcpu_id == 0 )
+                    {
+                        spin_lock(&iorp->lock);
+                        rc = hvm_replace_event_channel(v, a.value,
+                            (int*)&v->domain->arch.hvm_domain.params[HVM_PARAM_BUFIOREQ_EVTCHN]);
+                        spin_unlock(&iorp->lock);
+                        if ( rc )
+                            break;
                     }
-                    /* xchg() ensures that only we free_xen_event_channel() */
-                    old_port = xchg(&v->arch.hvm_vcpu.xen_port, new_port);
-                    free_xen_event_channel(v, old_port);
+
                     spin_lock(&iorp->lock);
                     if ( iorp->va != NULL )
                         get_ioreq(v)->vp_eport = v->arch.hvm_vcpu.xen_port;
Signature · 1 line
Anthony PERARD
Acked-by Tested-by on v2 1/1
#1 lore raw
Quoted 9 lines · Anthony PERARD

This is a missing part from the previous patch that add the BUFIOREQ_EVTCHN parameter. This patch changes the ownership of the buifioreq event channel to the stubdom (when HVM_PARAM_DM_DOMAIN is set within the stubdom).

This patch introduces an helper to replace a xen port.

This fix the initialization of QEMU inside the stubdomain.

Thought I'd already acked this but apparently not. This fixes stub domains for me.

CCing Hypervisor maintainers...

Quoted 65 lines

---
Change:
- an helper
- the replacement of the buferioreq evtchn is inside iorp->lock now.

 xen/arch/x86/hvm/hvm.c |   37 ++++++++++++++++++++++++++++---------
 1 files changed, 28 insertions(+), 9 deletions(-)
xen/arch/x86/hvm/hvm.c +28 -9
index e0d495d..e8e86c0 100644
@@ -3664,6 +3664,21 @@ static int hvmop_flush_tlb_all(void)
     return 0;
 }
 
+static int hvm_replace_event_channel(struct vcpu *v, uint64_t remote_domid,
+                                     int *port)
+{
+    int old_port, new_port;
+
+    new_port = alloc_unbound_xen_event_channel(v, remote_domid, NULL);
+    if ( new_port < 0 )
+        return new_port;
+
+    /* xchg() ensures that only we free_xen_event_channel() */
+    old_port = xchg(port, new_port);
+    free_xen_event_channel(v, old_port);
+    return 0;
+}
+
 long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE(void) arg)
 
 {
@@ -3777,17 +3792,21 @@ long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE(void) arg)
                 iorp = &d->arch.hvm_domain.ioreq;
                 for_each_vcpu ( d, v )
                 {
-                    int old_port, new_port;
-                    new_port = alloc_unbound_xen_event_channel(
-                        v, a.value, NULL);
-                    if ( new_port < 0 )
-                    {
-                        rc = new_port;
+                    rc = hvm_replace_event_channel(v, a.value,
+                                                   &v->arch.hvm_vcpu.xen_port);
+                    if ( rc )
                         break;
+
+                    if ( v->vcpu_id == 0 )
+                    {
+                        spin_lock(&iorp->lock);
+                        rc = hvm_replace_event_channel(v, a.value,
+                            (int*)&v->domain->arch.hvm_domain.params[HVM_PARAM_BUFIOREQ_EVTCHN]);
+                        spin_unlock(&iorp->lock);
+                        if ( rc )
+                            break;
                     }
-                    /* xchg() ensures that only we free_xen_event_channel() */
-                    old_port = xchg(&v->arch.hvm_vcpu.xen_port, new_port);
-                    free_xen_event_channel(v, old_port);
+
                     spin_lock(&iorp->lock);
                     if ( iorp->va != NULL )
                         get_ioreq(v)->vp_eport = v->arch.hvm_vcpu.xen_port;
#2 lore raw
Quoted 2 lines · Ian Campbell

xen/arch/x86/hvm/hvm.c static int hvmop_flush_tlb_all(void) · line 3668

Quoted 2 lines
xen/arch/x86/hvm/hvm.c +2 -0
+static int hvm_replace_event_channel(struct vcpu *v, uint64_t remote_domid,
+                                     int *port)

int *p_port?...

Quoted 9 lines

xen/arch/x86/hvm/hvm.c static int hvmop_flush_tlb_all(void) · line 3677

Quoted 9 lines
xen/arch/x86/hvm/hvm.c +9 -0
+{
+    int old_port, new_port;
+
+    new_port = alloc_unbound_xen_event_channel(v, remote_domid, NULL);
+    if ( new_port < 0 )
+        return new_port;
+
+    /* xchg() ensures that only we free_xen_event_channel() */
+    old_port = xchg(port, new_port);

...Would make this line a little bit clearer imo.

Quoted 4 lines
Quoted 4 lines
xen/arch/x86/hvm/hvm.c +4 -0
+    free_xen_event_channel(v, old_port);
+    return 0;
+}
+
#3 lore raw
In reply to Anthony PERARD
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Anthony PERARD
xen/arch/x86/hvm/hvm.c +0 -2
@@ -3777,17 +3792,21 @@ long do_hvm_op(unsigned long op, 
 XEN_GUEST_HANDLE(void) arg)
                 iorp = &d->arch.hvm_domain.ioreq;
                 for_each_vcpu ( d, v )
                 {
-                    int old_port, new_port;
-                    new_port = alloc_unbound_xen_event_channel(

xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, · line 3801

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +5 -4
-                        v, a.value, NULL);
-                    if ( new_port < 0 )
-                    {
-                        rc = new_port;
+                    rc = hvm_replace_event_channel(v, a.value,
+                                                   &v->arch.hvm_vcpu.xen_port);
+                    if ( rc )
                         break;
+
+                    if ( v->vcpu_id == 0 )

Don't you also have to check params[HVM_PARAM_BUFIOREQ_EVTCHN] is valid (as otherwise free_xen_event_channel() will BUG_ON() on it)?

xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE(void) arg) · line 3808

Quoted 8 lines
xen/arch/x86/hvm/hvm.c +7 -0
+                    {
+                        spin_lock(&iorp->lock);
+                        rc = hvm_replace_event_channel(v, a.value,
+                            (int*)&v->domain->arch.hvm_domain.params[HVM_PARAM_BUFIOREQ_EVTCHN]);
+                        spin_unlock(&iorp->lock);
+                        if ( rc )
+                            break;
                     }

My first preference would be for this to be moved out of the loop. If it has to remain in the loop for some reason, then the next best option would be to move this into the iorp->lock protected region immediately below.

Jan

Quoted 7 lines
xen/arch/x86/hvm/hvm.c +1 -3
-                    /* xchg() ensures that only we free_xen_event_channel() */
-                    old_port = xchg(&v->arch.hvm_vcpu.xen_port, new_port);
-                    free_xen_event_channel(v, old_port);
+
                     spin_lock(&iorp->lock);
                     if ( iorp->va != NULL )
                         get_ioreq(v)->vp_eport = v->arch.hvm_vcpu.xen_port;
#4 lore raw
Quoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · Anthony PERARD
xen/arch/x86/hvm/hvm.c +2 -6
@@ -3777,17 +3792,21 @@ long do_hvm_op(unsigned long op,
 XEN_GUEST_HANDLE(void) arg)
                 iorp = &d->arch.hvm_domain.ioreq;
                 for_each_vcpu ( d, v )
                 {
-                    int old_port, new_port;
-                    new_port = alloc_unbound_xen_event_channel(
-                        v, a.value, NULL);
-                    if ( new_port < 0 )
-                    {
-                        rc = new_port;
+                    rc = hvm_replace_event_channel(v, a.value,
+               
 &v->arch.hvm_vcpu.xen_port);
Quoted 8 lines

xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE(void) arg) · line 3800

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +3 -0
+                    if ( rc )
                         break;
+
+                    if ( v->vcpu_id == 0 )

Don't you also have to check params[HVM_PARAM_BUFIOREQ_EVTCHN] is valid (as otherwise free_xen_event_channel() will BUG_ON() on it)?

No, params[HVM_PARAM_BUFIOREQ_EVTCHN] is guaranteed valid.

Quoted 10 lines

xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE(void) arg) · line 3808

Quoted 9 lines
xen/arch/x86/hvm/hvm.c +7 -0
+                    {
+                        spin_lock(&iorp->lock);
+                        rc = hvm_replace_event_channel(v, a.value,
+               
 (int*)&v->domain->arch.hvm_domain.params[HVM_PARAM_BUFIOREQ_EVTCHN]);
+                        spin_unlock(&iorp->lock);
+                        if ( rc )
+                            break;
                     }
Quoted 4 lines

My first preference would be for this to be moved out of the loop. If it has to remain in the loop for some reason, then the next best option would be to move this into the iorp->lock protected region immediately below.

Agree on moving it out of the loop. But why would you want it protected by iorp->lock?

-- Keir

Quoted 17 lines

Jan

xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE(void) arg) · line 3812

Quoted 8 lines
xen/arch/x86/hvm/hvm.c +1 -3
-                    /* xchg() ensures that only we free_xen_event_channel()
 */
-                    old_port = xchg(&v->arch.hvm_vcpu.xen_port, new_port);
-                    free_xen_event_channel(v, old_port);
+
                     spin_lock(&iorp->lock);
                     if ( iorp->va != NULL )
                         get_ioreq(v)->vp_eport = v->arch.hvm_vcpu.xen_port;

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

#5 lore raw
Quoted 38 lines · Keir Fraser
Quoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · Anthony PERARD
xen/arch/x86/hvm/hvm.c +2 -6
@@ -3777,17 +3792,21 @@ long do_hvm_op(unsigned long op,
 XEN_GUEST_HANDLE(void) arg)
                 iorp = &d->arch.hvm_domain.ioreq;
                 for_each_vcpu ( d, v )
                 {
-                    int old_port, new_port;
-                    new_port = alloc_unbound_xen_event_channel(
-                        v, a.value, NULL);
-                    if ( new_port < 0 )
-                    {
-                        rc = new_port;
+                    rc = hvm_replace_event_channel(v, a.value,
+               
 &v->arch.hvm_vcpu.xen_port);
Quoted 8 lines

xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE(void) arg) · line 3800

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +3 -0
+                    if ( rc )
                         break;
+
+                    if ( v->vcpu_id == 0 )

Don't you also have to check params[HVM_PARAM_BUFIOREQ_EVTCHN] is valid (as otherwise free_xen_event_channel() will BUG_ON() on it)?

No, params[HVM_PARAM_BUFIOREQ_EVTCHN] is guaranteed valid.

Quoted 10 lines
Quoted 9 lines
xen/arch/x86/hvm/hvm.c +7 -0
+                    {
+                        spin_lock(&iorp->lock);
+                        rc = hvm_replace_event_channel(v, a.value,
+               
 (int*)&v->domain->arch.hvm_domain.params[HVM_PARAM_BUFIOREQ_EVTCHN]);
+                        spin_unlock(&iorp->lock);
+                        if ( rc )
+                            break;
                     }
Quoted 7 lines
Quoted 4 lines

My first preference would be for this to be moved out of the loop. If it has to remain in the loop for some reason, then the next best option would be to move this into the iorp->lock protected region immediately below.

Agree on moving it out of the loop. But why would you want it protected by iorp->lock?

I suggested it because the user of the field locks with that lock.

I think that even with the xchg there is still scope for the old event channel to be in use in hvm_buffered_io_send after it has been replaced. The xchg just protects against concurrent freeing.

Ian.

Quoted 22 lines

-- Keir

Quoted 17 lines

Jan

xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE(void) arg) · line 3812

Quoted 8 lines
xen/arch/x86/hvm/hvm.c +1 -3
-                    /* xchg() ensures that only we free_xen_event_channel()
 */
-                    old_port = xchg(&v->arch.hvm_vcpu.xen_port, new_port);
-                    free_xen_event_channel(v, old_port);
+
                     spin_lock(&iorp->lock);
                     if ( iorp->va != NULL )
                         get_ioreq(v)->vp_eport = v->arch.hvm_vcpu.xen_port;

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

#6 lore raw
Quoted 4 lines · Ian Campbell
Quoted 2 lines

Agree on moving it out of the loop. But why would you want it protected by iorp->lock?

I suggested it because the user of the field locks with that lock.

That lock is really protecting access to the shared bufioreq page. The evtchn notify could equally well be moved outside the lock.

Quoted 3 lines

I think that even with the xchg there is still scope for the old event channel to be in use in hvm_buffered_io_send after it has been replaced. The xchg just protects against concurrent freeing.

A. This would be equally true for the per-vcpu hvm_vcpu.xen_port; but B. We avoid such races by domain_pause()ing when changing HVM_PARAM_DM_DOMAIN; and
C. In practice we only set HVM_PARAM_DM_DOMAIN before the guest starts to run anyway.

-- Keir

#7 lore raw
Quoted 9 lines · Keir Fraser
Quoted 4 lines · Ian Campbell
Quoted 2 lines

Agree on moving it out of the loop. But why would you want it protected by iorp->lock?

I suggested it because the user of the field locks with that lock.

That lock is really protecting access to the shared bufioreq page. The evtchn notify could equally well be moved outside the lock.

OK.

Quoted 9 lines
Quoted 3 lines

I think that even with the xchg there is still scope for the old event channel to be in use in hvm_buffered_io_send after it has been replaced. The xchg just protects against concurrent freeing.

A. This would be equally true for the per-vcpu hvm_vcpu.xen_port; but B. We avoid such races by domain_pause()ing when changing HVM_PARAM_DM_DOMAIN; and
C. In practice we only set HVM_PARAM_DM_DOMAIN before the guest starts to run anyway.

I thought we locked the update of xen_port too -- but actually that's only the update of get_ioreq(v)->vp_eport, which is locked against iorp->va changing.

Ian.

#8 lore raw
In reply to Keir Fraser
Quoted 27 lines
Quoted 1 line
Quoted 1 line · Keir Fraser
Quoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · Anthony PERARD
xen/arch/x86/hvm/hvm.c +2 -6
@@ -3777,17 +3792,21 @@ long do_hvm_op(unsigned long op,
 XEN_GUEST_HANDLE(void) arg)
                 iorp = &d->arch.hvm_domain.ioreq;
                 for_each_vcpu ( d, v )
                 {
-                    int old_port, new_port;
-                    new_port = alloc_unbound_xen_event_channel(
-                        v, a.value, NULL);
-                    if ( new_port < 0 )
-                    {
-                        rc = new_port;
+                    rc = hvm_replace_event_channel(v, a.value,
+               
 &v->arch.hvm_vcpu.xen_port);
Quoted 8 lines

xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE(void) arg) · line 3800

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +3 -0
+                    if ( rc )
                         break;
+
+                    if ( v->vcpu_id == 0 )

Don't you also have to check params[HVM_PARAM_BUFIOREQ_EVTCHN] is valid (as otherwise free_xen_event_channel() will BUG_ON() on it)?

Quoted 1 line

No, params[HVM_PARAM_BUFIOREQ_EVTCHN] is guaranteed valid.

Oh, I see, this is being set by hvm_vcpu_initialize(), and read-only to any external entity.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
xen/arch/x86/hvm/hvm.c +7 -0
+                    {
+                        spin_lock(&iorp->lock);
+                        rc = hvm_replace_event_channel(v, a.value,
+               
 (int*)&v->domain->arch.hvm_domain.params[HVM_PARAM_BUFIOREQ_EVTCHN]);
+                        spin_unlock(&iorp->lock);
+                        if ( rc )
+                            break;
                     }
Quoted 7 lines
Quoted 4 lines

My first preference would be for this to be moved out of the loop. If it has to remain in the loop for some reason, then the next best option would be to move this into the iorp->lock protected region immediately below.

Agree on moving it out of the loop. But why would you want it protected by iorp->lock?

That's a question to Anthony - I just saw that the same lock is being used here and a few lines down.

Jan

#9 lore raw
Quoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 14 lines

xen/arch/x86/hvm/hvm.c long do_hvm_op(unsigned long op, XEN_GUEST_HANDLE(void) arg) · line 3808

Quoted 9 lines
xen/arch/x86/hvm/hvm.c +4 -0
+                    {
+                        spin_lock(&iorp->lock);
+                        rc = hvm_replace_event_channel(v, a.value,
+             

(int*)&v->domain->arch.hvm_domain.params[HVM_PARAM_BUFIOREQ_EVTCHN]);

xen/arch/x86/hvm/hvm.c +3 -0
+                        spin_unlock(&iorp->lock);
+                        if ( rc )
+                            break;
                     }

My first preference would be for this to be moved out of the loop. If it has to remain in the loop for some reason, then the next best option would be to move this into the iorp->lock protected region immediately below.

Quoted 5 lines
Quoted 2 lines

Agree on moving it out of the loop. But why would you want it protected by iorp->lock?

That's a question to Anthony - I just saw that the same lock is being used here and a few lines down.

Ah, I see. It is not necessary to take the lock in the above code fragment.

-- Keir

Quoted 2 lines

Jan