Skip to contentSkip to conversation

xen_pt: fix failure of attaching & detaching a PCI device to VM repeatedly

PATCH Superseded Jianzhong,Chang 2 participants 3 messages Dec 9, 2015

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xen_pt: fix failure of attaching & detaching a PCI device to VM repeatedly

Add pci = [ '$VF_BDF1', '$VF_BDF2', '$VF_BDF3'] in
hvm guest configuration file. After the guest boot up,
detach the VFs in sequence by
"xl pci-detach $DOMID $VF_BDF1"
"xl pci-detach $DOMID $VF_BDF2"
"xl pci-detach $DOMID $VF_BDF3"
and reattach the VFs in sequence by
"xl pci-attach $DOMID $VF_BDF1"
"xl pci-attach $DOMID $VF_BDF2"
"xl pci-attach $DOMID $VF_BDF3"
An error message will be reported like this:
"libxl: error: libxl_qmp.c:287:qmp_handle_error_response: received an error message from QMP server: Duplicate ID 'pci-pt-07_10.1' for device"

When xen_pt_region_add/del() is called, MemoryRegion
may not belong to the XenPCIPassthroughState. xen_pt_region_update() checks it but memory_region_ref/unref() does not. This case causes obj->ref issue and affects the release of related objects. So, memory_region_ref/unref() is moved from xen_pt_region_add/del to xen_pt_region_update.

---
 hw/xen/xen_pt.c |   13 ++++++++-----
 1 files changed, 8 insertions(+), 5 deletions(-)
hw/xen/xen_pt.c +8 -5
index aa96288..b963208 100644
@@ -590,11 +590,15 @@ static void xen_pt_region_update(XenPCIPassthroughState *s,
     if (bar == -1 && (!s->msix || &s->msix->mmio != mr)) {
         return;
     }
-
+    if (adding) {
+        memory_region_ref(mr);
+    }
     if (s->msix && &s->msix->mmio == mr) {
         if (adding) {
             s->msix->mmio_base_addr = sec->offset_within_address_space;
             rc = xen_pt_msix_update_remap(s, s->msix->bar_index);
+        } else {
+            memory_region_unref(mr);
         }
         return;
     }
@@ -635,6 +639,9 @@ static void xen_pt_region_update(XenPCIPassthroughState *s,
                        adding ? "create new" : "remove old", errno);
         }
     }
+    if (!adding) {
+        memory_region_unref(mr);
+    }
 }
 
 static void xen_pt_region_add(MemoryListener *l, MemoryRegionSection *sec)
@@ -642,7 +649,6 @@ static void xen_pt_region_add(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              memory_listener);
 
-    memory_region_ref(sec->mr);
     xen_pt_region_update(s, sec, true);
 }
 
@@ -652,7 +658,6 @@ static void xen_pt_region_del(MemoryListener *l, MemoryRegionSection *sec)
                                              memory_listener);
 
     xen_pt_region_update(s, sec, false);
-    memory_region_unref(sec->mr);
 }
 
 static void xen_pt_io_region_add(MemoryListener *l, MemoryRegionSection *sec)
@@ -660,7 +665,6 @@ static void xen_pt_io_region_add(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              io_listener);
 
-    memory_region_ref(sec->mr);
     xen_pt_region_update(s, sec, true);
 }
 
@@ -670,7 +674,6 @@ static void xen_pt_io_region_del(MemoryListener *l, MemoryRegionSection *sec)
                                              io_listener);
 
     xen_pt_region_update(s, sec, false);
-    memory_region_unref(sec->mr);
 }
 
 static const MemoryListener xen_pt_memory_listener = {
Signature · 1 line
1.7.1
#1 lore raw
Quoted 14 lines · Jianzhong,Chang

Add pci = [ '$VF_BDF1', '$VF_BDF2', '$VF_BDF3'] in
hvm guest configuration file. After the guest boot up,
detach the VFs in sequence by
"xl pci-detach $DOMID $VF_BDF1"
"xl pci-detach $DOMID $VF_BDF2"
"xl pci-detach $DOMID $VF_BDF3"
and reattach the VFs in sequence by
"xl pci-attach $DOMID $VF_BDF1"
"xl pci-attach $DOMID $VF_BDF2"
"xl pci-attach $DOMID $VF_BDF3"
An error message will be reported like this:
"libxl: error: libxl_qmp.c:287:qmp_handle_error_response: received an error message from QMP server: Duplicate ID 'pci-pt-07_10.1' for device"

Quoted 8 lines

When xen_pt_region_add/del() is called, MemoryRegion
may not belong to the XenPCIPassthroughState. xen_pt_region_update() checks it but memory_region_ref/unref() does not. This case causes obj->ref issue and affects the release of related objects. So, memory_region_ref/unref() is moved from xen_pt_region_add/del to xen_pt_region_update.

This is much better, thanks! I have just one minor ask, see below.

Quoted 12 lines
 hw/xen/xen_pt.c |   13 ++++++++-----
 1 files changed, 8 insertions(+), 5 deletions(-)
hw/xen/xen_pt.c +0 -1
index aa96288..b963208 100644
@@ -590,11 +590,15 @@ static void xen_pt_region_update(XenPCIPassthroughState *s,
     if (bar == -1 && (!s->msix || &s->msix->mmio != mr)) {
         return;
     }
-

hw/xen/xen_pt.c static void xen_pt_region_update(XenPCIPassthroughState *s, · line 602

Quoted 10 lines
hw/xen/xen_pt.c +5 -0
+    if (adding) {
+        memory_region_ref(mr);
+    }
     if (s->msix && &s->msix->mmio == mr) {
         if (adding) {
             s->msix->mmio_base_addr = sec->offset_within_address_space;
             rc = xen_pt_msix_update_remap(s, s->msix->bar_index);
+        } else {
+            memory_region_unref(mr);
         }

Instead of this, could you please add an out label below, where you call memory_region_unref, and here just goto out;

Quoted 47 lines
hw/xen/xen_pt.c +3 -4
         return;
     }
@@ -635,6 +639,9 @@ static void xen_pt_region_update(XenPCIPassthroughState *s,
                        adding ? "create new" : "remove old", errno);
         }
     }
+    if (!adding) {
+        memory_region_unref(mr);
+    }
 }
 
 static void xen_pt_region_add(MemoryListener *l, MemoryRegionSection *sec)
@@ -642,7 +649,6 @@ static void xen_pt_region_add(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              memory_listener);
 
-    memory_region_ref(sec->mr);
     xen_pt_region_update(s, sec, true);
 }
 
@@ -652,7 +658,6 @@ static void xen_pt_region_del(MemoryListener *l, MemoryRegionSection *sec)
                                              memory_listener);
 
     xen_pt_region_update(s, sec, false);
-    memory_region_unref(sec->mr);
 }
 
 static void xen_pt_io_region_add(MemoryListener *l, MemoryRegionSection *sec)
@@ -660,7 +665,6 @@ static void xen_pt_io_region_add(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              io_listener);
 
-    memory_region_ref(sec->mr);
     xen_pt_region_update(s, sec, true);
 }
 
@@ -670,7 +674,6 @@ static void xen_pt_io_region_del(MemoryListener *l, MemoryRegionSection *sec)
                                              io_listener);
 
     xen_pt_region_update(s, sec, false);
-    memory_region_unref(sec->mr);
 }
 
 static const MemoryListener xen_pt_memory_listener = {
Signature · 1 line
1.7.1
#2 lore raw
In reply to Jianzhong,Chang
Quoted 14 lines · Jianzhong,Chang

Add pci = [ '$VF_BDF1', '$VF_BDF2', '$VF_BDF3'] in
hvm guest configuration file. After the guest boot up,
detach the VFs in sequence by
"xl pci-detach $DOMID $VF_BDF1"
"xl pci-detach $DOMID $VF_BDF2"
"xl pci-detach $DOMID $VF_BDF3"
and reattach the VFs in sequence by
"xl pci-attach $DOMID $VF_BDF1"
"xl pci-attach $DOMID $VF_BDF2"
"xl pci-attach $DOMID $VF_BDF3"
An error message will be reported like this:
"libxl: error: libxl_qmp.c:287:qmp_handle_error_response: received an error message from QMP server: Duplicate ID 'pci-pt-07_10.1' for device"

Quoted 8 lines

When xen_pt_region_add/del() is called, MemoryRegion
may not belong to the XenPCIPassthroughState. xen_pt_region_update() checks it but memory_region_ref/unref() does not. This case causes obj->ref issue and affects the release of related objects. So, memory_region_ref/unref() is moved from xen_pt_region_add/del to xen_pt_region_update.

This is much better, thanks! I have just one minor ask, see below.

Quoted 12 lines
 hw/xen/xen_pt.c |   13 ++++++++-----
 1 files changed, 8 insertions(+), 5 deletions(-)
hw/xen/xen_pt.c +0 -1
index aa96288..b963208 100644
@@ -590,11 +590,15 @@ static void xen_pt_region_update(XenPCIPassthroughState *s,
     if (bar == -1 && (!s->msix || &s->msix->mmio != mr)) {
         return;
     }
-

hw/xen/xen_pt.c static void xen_pt_region_update(XenPCIPassthroughState *s, · line 602

Quoted 10 lines
hw/xen/xen_pt.c +5 -0
+    if (adding) {
+        memory_region_ref(mr);
+    }
     if (s->msix && &s->msix->mmio == mr) {
         if (adding) {
             s->msix->mmio_base_addr = sec->offset_within_address_space;
             rc = xen_pt_msix_update_remap(s, s->msix->bar_index);
+        } else {
+            memory_region_unref(mr);
         }

Instead of this, could you please add an out label below, where you call memory_region_unref, and here just goto out;

Quoted 47 lines
hw/xen/xen_pt.c +3 -4
         return;
     }
@@ -635,6 +639,9 @@ static void xen_pt_region_update(XenPCIPassthroughState *s,
                        adding ? "create new" : "remove old", errno);
         }
     }
+    if (!adding) {
+        memory_region_unref(mr);
+    }
 }
 
 static void xen_pt_region_add(MemoryListener *l, MemoryRegionSection *sec)
@@ -642,7 +649,6 @@ static void xen_pt_region_add(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              memory_listener);
 
-    memory_region_ref(sec->mr);
     xen_pt_region_update(s, sec, true);
 }
 
@@ -652,7 +658,6 @@ static void xen_pt_region_del(MemoryListener *l, MemoryRegionSection *sec)
                                              memory_listener);
 
     xen_pt_region_update(s, sec, false);
-    memory_region_unref(sec->mr);
 }
 
 static void xen_pt_io_region_add(MemoryListener *l, MemoryRegionSection *sec)
@@ -660,7 +665,6 @@ static void xen_pt_io_region_add(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              io_listener);
 
-    memory_region_ref(sec->mr);
     xen_pt_region_update(s, sec, true);
 }
 
@@ -670,7 +674,6 @@ static void xen_pt_io_region_del(MemoryListener *l, MemoryRegionSection *sec)
                                              io_listener);
 
     xen_pt_region_update(s, sec, false);
-    memory_region_unref(sec->mr);
 }
 
 static const MemoryListener xen_pt_memory_listener = {
Signature · 1 line
1.7.1