Skip to contentSkip to conversation

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

PATCH In review Jianzhong,Chang 3 participants 9 messages Dec 3, 2015 – Dec 7, 2015

lore
Share & tools
lore
#0 lore raw

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

From: jianzhong,Chang <[email protected]>

Add pci = [ '$VF_BDF', '$VF_BDF', '$VF_BDF'] in
hvm guest configuration file. After the guest boot up,
detach the VFs in sequence by "xl pci-detach $DOMID $VF_BDF", reattach the VFs by "xl pci-attach $VF_BDF" in sequence.
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, the detection operation is moved from
xen_pt_region_update to xen_pt_region_add/del.

---
 hw/xen/xen_pt.c |   23 ++++++++++++++++++++---
 1 files changed, 20 insertions(+), 3 deletions(-)
hw/xen/xen_pt.c +20 -3
index aa96288..95b4970 100644
@@ -523,6 +523,14 @@ static int xen_pt_bar_from_region(XenPCIPassthroughState *s, MemoryRegion *mr)
     }
     return -1;
 }
+static bool xen_pt_region_in_state(XenPCIPassthroughState *s, MemoryRegion *mr)
+{
+    int bar = xen_pt_bar_from_region(s, mr);
+    if (-1 == bar && (!s->msix || &s->msix->mmio != mr)) {
+        return false;
+    }
+    return true;
+}
 
 /*
  * This function checks if an io_region overlaps an io_region from another
@@ -587,9 +595,6 @@ static void xen_pt_region_update(XenPCIPassthroughState *s,
     };
 
     bar = xen_pt_bar_from_region(s, mr);
-    if (bar == -1 && (!s->msix || &s->msix->mmio != mr)) {
-        return;
-    }
 
     if (s->msix && &s->msix->mmio == mr) {
         if (adding) {
@@ -642,6 +647,9 @@ static void xen_pt_region_add(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              memory_listener);
 
+    if (!xen_pt_region_in_state(s, sec->mr)) {
+        return;
+    }
     memory_region_ref(sec->mr);
     xen_pt_region_update(s, sec, true);
 }
@@ -651,6 +659,9 @@ static void xen_pt_region_del(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              memory_listener);
 
+    if (!xen_pt_region_in_state(s, sec->mr)) {
+        return;
+    }
     xen_pt_region_update(s, sec, false);
     memory_region_unref(sec->mr);
 }
@@ -660,6 +671,9 @@ static void xen_pt_io_region_add(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              io_listener);
 
+    if (!xen_pt_region_in_state(s, sec->mr)) {
+        return;
+    }
     memory_region_ref(sec->mr);
     xen_pt_region_update(s, sec, true);
 }
@@ -669,6 +683,9 @@ static void xen_pt_io_region_del(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              io_listener);
 
+    if (!xen_pt_region_in_state(s, sec->mr)) {
+        return;
+    }
     xen_pt_region_update(s, sec, false);
     memory_region_unref(sec->mr);
 }
Signature · 1 line
1.7.1
#1 lore raw
Quoted 3 lines · Jianzhong,Chang

From: jianzhong,Chang <[email protected]>

Add pci = [ '$VF_BDF', '$VF_BDF', '$VF_BDF'] in

This is a bit confusing: it is not actually correct to assign the same device, even an SR_IOV VF, multiple times, so these must be all different. More like:

pci = [ '$VF_BDF1', '$VF_BDF2', '$VF_BDF3']

Quoted 3 lines

hvm guest configuration file. After the guest boot up, detach the VFs in sequence by "xl pci-detach $DOMID $VF_BDF", reattach the VFs by "xl pci-attach $VF_BDF" in sequence.

So do you mean:

xl pci-detach $DOMID $VF_BDF1
xl pci-detach $DOMID $VF_BDF2
xl pci-detach $DOMID $VF_BDF3
xl pci-attach $DOMID $VF_BDF1
xl pci-attach $DOMID $VF_BDF2
xl pci-attach $DOMID $VF_BDF3

?

Quoted 76 lines

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, the detection operation is moved from
xen_pt_region_update to xen_pt_region_add/del.

---
 hw/xen/xen_pt.c |   23 ++++++++++++++++++++---
 1 files changed, 20 insertions(+), 3 deletions(-)
hw/xen/xen_pt.c +17 -3
index aa96288..95b4970 100644
@@ -523,6 +523,14 @@ static int xen_pt_bar_from_region(XenPCIPassthroughState *s, MemoryRegion *mr)
     }
     return -1;
 }
+static bool xen_pt_region_in_state(XenPCIPassthroughState *s, MemoryRegion *mr)
+{
+    int bar = xen_pt_bar_from_region(s, mr);
+    if (-1 == bar && (!s->msix || &s->msix->mmio != mr)) {
+        return false;
+    }
+    return true;
+}
 
 /*
  * This function checks if an io_region overlaps an io_region from another
@@ -587,9 +595,6 @@ static void xen_pt_region_update(XenPCIPassthroughState *s,
     };
 
     bar = xen_pt_bar_from_region(s, mr);
-    if (bar == -1 && (!s->msix || &s->msix->mmio != mr)) {
-        return;
-    }
 
     if (s->msix && &s->msix->mmio == mr) {
         if (adding) {
@@ -642,6 +647,9 @@ static void xen_pt_region_add(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              memory_listener);
 
+    if (!xen_pt_region_in_state(s, sec->mr)) {
+        return;
+    }
     memory_region_ref(sec->mr);
     xen_pt_region_update(s, sec, true);
 }
@@ -651,6 +659,9 @@ static void xen_pt_region_del(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              memory_listener);
 
+    if (!xen_pt_region_in_state(s, sec->mr)) {
+        return;
+    }
     xen_pt_region_update(s, sec, false);
     memory_region_unref(sec->mr);
 }
@@ -660,6 +671,9 @@ static void xen_pt_io_region_add(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              io_listener);
 
+    if (!xen_pt_region_in_state(s, sec->mr)) {
+        return;
+    }
     memory_region_ref(sec->mr);
     xen_pt_region_update(s, sec, true);
 }

hw/xen/xen_pt.c static void xen_pt_io_region_del(MemoryListener *l, MemoryRegionSection *sec) · line 691

Quoted 9 lines
hw/xen/xen_pt.c +3 -0
@@ -669,6 +683,9 @@ static void xen_pt_io_region_del(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              io_listener);
 
+    if (!xen_pt_region_in_state(s, sec->mr)) {
+        return;
+    }
     xen_pt_region_update(s, sec, false);
     memory_region_unref(sec->mr);
 }

Wouldn't it make more sense to move the memory_region_ref/memory_region_unref calls inside xen_pt_region_update?

#2 lore raw
In reply to Jianzhong,Chang
Quoted 3 lines · Jianzhong,Chang

From: jianzhong,Chang <[email protected]>

Add pci = [ '$VF_BDF', '$VF_BDF', '$VF_BDF'] in

This is a bit confusing: it is not actually correct to assign the same device, even an SR_IOV VF, multiple times, so these must be all different. More like:

pci = [ '$VF_BDF1', '$VF_BDF2', '$VF_BDF3']

Quoted 3 lines

hvm guest configuration file. After the guest boot up, detach the VFs in sequence by "xl pci-detach $DOMID $VF_BDF", reattach the VFs by "xl pci-attach $VF_BDF" in sequence.

So do you mean:

xl pci-detach $DOMID $VF_BDF1
xl pci-detach $DOMID $VF_BDF2
xl pci-detach $DOMID $VF_BDF3
xl pci-attach $DOMID $VF_BDF1
xl pci-attach $DOMID $VF_BDF2
xl pci-attach $DOMID $VF_BDF3

?

Quoted 76 lines

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, the detection operation is moved from
xen_pt_region_update to xen_pt_region_add/del.

---
 hw/xen/xen_pt.c |   23 ++++++++++++++++++++---
 1 files changed, 20 insertions(+), 3 deletions(-)
hw/xen/xen_pt.c +17 -3
index aa96288..95b4970 100644
@@ -523,6 +523,14 @@ static int xen_pt_bar_from_region(XenPCIPassthroughState *s, MemoryRegion *mr)
     }
     return -1;
 }
+static bool xen_pt_region_in_state(XenPCIPassthroughState *s, MemoryRegion *mr)
+{
+    int bar = xen_pt_bar_from_region(s, mr);
+    if (-1 == bar && (!s->msix || &s->msix->mmio != mr)) {
+        return false;
+    }
+    return true;
+}
 
 /*
  * This function checks if an io_region overlaps an io_region from another
@@ -587,9 +595,6 @@ static void xen_pt_region_update(XenPCIPassthroughState *s,
     };
 
     bar = xen_pt_bar_from_region(s, mr);
-    if (bar == -1 && (!s->msix || &s->msix->mmio != mr)) {
-        return;
-    }
 
     if (s->msix && &s->msix->mmio == mr) {
         if (adding) {
@@ -642,6 +647,9 @@ static void xen_pt_region_add(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              memory_listener);
 
+    if (!xen_pt_region_in_state(s, sec->mr)) {
+        return;
+    }
     memory_region_ref(sec->mr);
     xen_pt_region_update(s, sec, true);
 }
@@ -651,6 +659,9 @@ static void xen_pt_region_del(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              memory_listener);
 
+    if (!xen_pt_region_in_state(s, sec->mr)) {
+        return;
+    }
     xen_pt_region_update(s, sec, false);
     memory_region_unref(sec->mr);
 }
@@ -660,6 +671,9 @@ static void xen_pt_io_region_add(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              io_listener);
 
+    if (!xen_pt_region_in_state(s, sec->mr)) {
+        return;
+    }
     memory_region_ref(sec->mr);
     xen_pt_region_update(s, sec, true);
 }

hw/xen/xen_pt.c static void xen_pt_io_region_del(MemoryListener *l, MemoryRegionSection *sec) · line 691

Quoted 9 lines
hw/xen/xen_pt.c +3 -0
@@ -669,6 +683,9 @@ static void xen_pt_io_region_del(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              io_listener);
 
+    if (!xen_pt_region_in_state(s, sec->mr)) {
+        return;
+    }
     xen_pt_region_update(s, sec, false);
     memory_region_unref(sec->mr);
 }

Wouldn't it make more sense to move the memory_region_ref/memory_region_unref calls inside xen_pt_region_update?

#3 lore raw
Quoted 14 lines
Quoted 1 line

Add pci = [ '$VF_BDF', '$VF_BDF', '$VF_BDF'] in

This is a bit confusing: it is not actually correct to assign the same device, even an SR_IOV VF, multiple times, so these must be all different. More like:

pci = [ '$VF_BDF1', '$VF_BDF2', '$VF_BDF3']

Quoted 3 lines

hvm guest configuration file. After the guest boot up, detach the VFs in sequence by "xl pci-detach $DOMID $VF_BDF", reattach the VFs by "xl pci-attach $VF_BDF" in sequence.

So do you mean:

Quoted 8 lines

xl pci-detach $DOMID $VF_BDF1
xl pci-detach $DOMID $VF_BDF2
xl pci-detach $DOMID $VF_BDF3
xl pci-attach $DOMID $VF_BDF1
xl pci-attach $DOMID $VF_BDF2
xl pci-attach $DOMID $VF_BDF3

?

I know this bug, I had a patch to fix a similar bug, I think Jianzhong means:

xl pci-detach $DOMID $VF_BDF1
xl pci-attach $DOMID $VF_BDF1
xl pci-detach $DOMID $VF_BDF1
xl pci-attach $DOMID $VF_BDF1
...

Liang

#4 lore raw
In reply to Stefano Stabellini
Quoted 14 lines
Quoted 1 line

Add pci = [ '$VF_BDF', '$VF_BDF', '$VF_BDF'] in

This is a bit confusing: it is not actually correct to assign the same device, even an SR_IOV VF, multiple times, so these must be all different. More like:

pci = [ '$VF_BDF1', '$VF_BDF2', '$VF_BDF3']

Quoted 3 lines

hvm guest configuration file. After the guest boot up, detach the VFs in sequence by "xl pci-detach $DOMID $VF_BDF", reattach the VFs by "xl pci-attach $VF_BDF" in sequence.

So do you mean:

Quoted 8 lines

xl pci-detach $DOMID $VF_BDF1
xl pci-detach $DOMID $VF_BDF2
xl pci-detach $DOMID $VF_BDF3
xl pci-attach $DOMID $VF_BDF1
xl pci-attach $DOMID $VF_BDF2
xl pci-attach $DOMID $VF_BDF3

?

I know this bug, I had a patch to fix a similar bug, I think Jianzhong means:

xl pci-detach $DOMID $VF_BDF1
xl pci-attach $DOMID $VF_BDF1
xl pci-detach $DOMID $VF_BDF1
xl pci-attach $DOMID $VF_BDF1
...

Liang

#5 lore raw

Bug url : http://bugzilla-archived.xenproject.org/bugzilla/show_bug.cgi?id=1905
Above url descripts the bug which i fix in more detail.
Add pci = [ '$VF_BDF', '$VF_BDF', '$VF_BDF'], $VF_BDFs are different. pci = [ '$VF_BDF1', '$VF_BDF2', '$VF_BDF3'] is a more accurate description.

-----Original Message-----
From: Li, Liang Z
Sent: Sunday, December 06, 2015 11:05 PM
To: Stefano Stabellini; Chang, JianzhongX
Cc: [email protected]; [email protected]; [email protected]; Lan, Tianyu; [email protected]
Subject: RE: [PATCH] xen_pt: fix failure of attaching & detaching a PCI device to VM repeatedly

Quoted 14 lines
Quoted 1 line

Add pci = [ '$VF_BDF', '$VF_BDF', '$VF_BDF'] in

This is a bit confusing: it is not actually correct to assign the same device, even an SR_IOV VF, multiple times, so these must be all different. More like:

pci = [ '$VF_BDF1', '$VF_BDF2', '$VF_BDF3']

Quoted 3 lines

hvm guest configuration file. After the guest boot up, detach the VFs in sequence by "xl pci-detach $DOMID $VF_BDF", reattach the VFs by "xl pci-attach $VF_BDF" in sequence.

So do you mean:

Quoted 8 lines

xl pci-detach $DOMID $VF_BDF1
xl pci-detach $DOMID $VF_BDF2
xl pci-detach $DOMID $VF_BDF3
xl pci-attach $DOMID $VF_BDF1
xl pci-attach $DOMID $VF_BDF2
xl pci-attach $DOMID $VF_BDF3

?

I know this bug, I had a patch to fix a similar bug, I think Jianzhong means:

xl pci-detach $DOMID $VF_BDF1
xl pci-attach $DOMID $VF_BDF1
xl pci-detach $DOMID $VF_BDF1
xl pci-attach $DOMID $VF_BDF1
...

Liang

#6 lore raw
In reply to Li, Liang Z

Bug url : http://bugzilla-archived.xenproject.org/bugzilla/show_bug.cgi?id=1905
Above url descripts the bug which i fix in more detail.
Add pci = [ '$VF_BDF', '$VF_BDF', '$VF_BDF'], $VF_BDFs are different. pci = [ '$VF_BDF1', '$VF_BDF2', '$VF_BDF3'] is a more accurate description.

-----Original Message-----
From: Li, Liang Z
Sent: Sunday, December 06, 2015 11:05 PM
To: Stefano Stabellini; Chang, JianzhongX
Cc: [email protected]; [email protected]; [email protected]; Lan, Tianyu; [email protected]
Subject: RE: [PATCH] xen_pt: fix failure of attaching & detaching a PCI device to VM repeatedly

Quoted 14 lines
Quoted 1 line

Add pci = [ '$VF_BDF', '$VF_BDF', '$VF_BDF'] in

This is a bit confusing: it is not actually correct to assign the same device, even an SR_IOV VF, multiple times, so these must be all different. More like:

pci = [ '$VF_BDF1', '$VF_BDF2', '$VF_BDF3']

Quoted 3 lines

hvm guest configuration file. After the guest boot up, detach the VFs in sequence by "xl pci-detach $DOMID $VF_BDF", reattach the VFs by "xl pci-attach $VF_BDF" in sequence.

So do you mean:

Quoted 8 lines

xl pci-detach $DOMID $VF_BDF1
xl pci-detach $DOMID $VF_BDF2
xl pci-detach $DOMID $VF_BDF3
xl pci-attach $DOMID $VF_BDF1
xl pci-attach $DOMID $VF_BDF2
xl pci-attach $DOMID $VF_BDF3

?

I know this bug, I had a patch to fix a similar bug, I think Jianzhong means:

xl pci-detach $DOMID $VF_BDF1
xl pci-attach $DOMID $VF_BDF1
xl pci-detach $DOMID $VF_BDF1
xl pci-attach $DOMID $VF_BDF1
...

Liang

#7 lore raw
In reply to Stefano Stabellini
Quoted 77 lines
Quoted 77 lines

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, the detection operation is moved from xen_pt_region_update to xen_pt_region_add/del.

---
 hw/xen/xen_pt.c |   23 ++++++++++++++++++++---
 1 files changed, 20 insertions(+), 3 deletions(-)
hw/xen/xen_pt.c index aa96288..95b4970 renamed from hw/xen/xen_pt.c +0 -0

100644

hw/xen/xen_pt.c +8 -0
@@ -523,6 +523,14 @@ static int xen_pt_bar_from_region(XenPCIPassthroughState *s, MemoryRegion *mr)
     }
     return -1;
 }
+static bool xen_pt_region_in_state(XenPCIPassthroughState *s, 
+MemoryRegion *mr) {
+    int bar = xen_pt_bar_from_region(s, mr);
+    if (-1 == bar && (!s->msix || &s->msix->mmio != mr)) {
+        return false;
+    }
+    return true;
+}
 
 /*
  * This function checks if an io_region overlaps an io_region from 

another @@ -587,9 +595,6 @@ static void xen_pt_region_update(XenPCIPassthroughState *s,

     };
 
     bar = xen_pt_bar_from_region(s, mr);
hw/xen/xen_pt.c +3 -3
-    if (bar == -1 && (!s->msix || &s->msix->mmio != mr)) {
-        return;
-    }
 
     if (s->msix && &s->msix->mmio == mr) {
         if (adding) {
@@ -642,6 +647,9 @@ static void xen_pt_region_add(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              memory_listener);
 
+    if (!xen_pt_region_in_state(s, sec->mr)) {
+        return;
+    }
     memory_region_ref(sec->mr);
     xen_pt_region_update(s, sec, true);  } @@ -651,6 +659,9 @@ static 
 void xen_pt_region_del(MemoryListener *l, MemoryRegionSection *sec)

XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
memory_listener);

hw/xen/xen_pt.c +6 -0
+    if (!xen_pt_region_in_state(s, sec->mr)) {
+        return;
+    }
     xen_pt_region_update(s, sec, false);
     memory_region_unref(sec->mr);
 }
@@ -660,6 +671,9 @@ static void xen_pt_io_region_add(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              io_listener);
 
+    if (!xen_pt_region_in_state(s, sec->mr)) {
+        return;
+    }
     memory_region_ref(sec->mr);
     xen_pt_region_update(s, sec, true);  } @@ -669,6 +683,9 @@ static 
 void xen_pt_io_region_del(MemoryListener *l, MemoryRegionSection *sec)

XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
io_listener);

Quoted 9 lines

hw/xen/xen_pt.c static void xen_pt_region_del(MemoryListener *l, MemoryRegionSection *sec) · line 667

Quoted 6 lines
hw/xen/xen_pt.c +3 -0
+    if (!xen_pt_region_in_state(s, sec->mr)) {
+        return;
+    }
     xen_pt_region_update(s, sec, false);
     memory_region_unref(sec->mr);
 }

Wouldn't it make more sense to move the memory_region_ref/memory_region_unref calls inside xen_pt_region_update?

Move the operation of mr->ref inside xen_pt_region_update can also fix this bug. For the convenience of maintenance, another patch can be created.

#8 lore raw
In reply to Stefano Stabellini
Quoted 77 lines
Quoted 77 lines

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, the detection operation is moved from xen_pt_region_update to xen_pt_region_add/del.

---
 hw/xen/xen_pt.c |   23 ++++++++++++++++++++---
 1 files changed, 20 insertions(+), 3 deletions(-)
hw/xen/xen_pt.c index aa96288..95b4970 renamed from hw/xen/xen_pt.c +0 -0

100644

hw/xen/xen_pt.c +8 -0
@@ -523,6 +523,14 @@ static int xen_pt_bar_from_region(XenPCIPassthroughState *s, MemoryRegion *mr)
     }
     return -1;
 }
+static bool xen_pt_region_in_state(XenPCIPassthroughState *s, 
+MemoryRegion *mr) {
+    int bar = xen_pt_bar_from_region(s, mr);
+    if (-1 == bar && (!s->msix || &s->msix->mmio != mr)) {
+        return false;
+    }
+    return true;
+}
 
 /*
  * This function checks if an io_region overlaps an io_region from 

another @@ -587,9 +595,6 @@ static void xen_pt_region_update(XenPCIPassthroughState *s,

     };
 
     bar = xen_pt_bar_from_region(s, mr);
hw/xen/xen_pt.c +3 -3
-    if (bar == -1 && (!s->msix || &s->msix->mmio != mr)) {
-        return;
-    }
 
     if (s->msix && &s->msix->mmio == mr) {
         if (adding) {
@@ -642,6 +647,9 @@ static void xen_pt_region_add(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              memory_listener);
 
+    if (!xen_pt_region_in_state(s, sec->mr)) {
+        return;
+    }
     memory_region_ref(sec->mr);
     xen_pt_region_update(s, sec, true);  } @@ -651,6 +659,9 @@ static 
 void xen_pt_region_del(MemoryListener *l, MemoryRegionSection *sec)

XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
memory_listener);

hw/xen/xen_pt.c +6 -0
+    if (!xen_pt_region_in_state(s, sec->mr)) {
+        return;
+    }
     xen_pt_region_update(s, sec, false);
     memory_region_unref(sec->mr);
 }
@@ -660,6 +671,9 @@ static void xen_pt_io_region_add(MemoryListener *l, MemoryRegionSection *sec)
     XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
                                              io_listener);
 
+    if (!xen_pt_region_in_state(s, sec->mr)) {
+        return;
+    }
     memory_region_ref(sec->mr);
     xen_pt_region_update(s, sec, true);  } @@ -669,6 +683,9 @@ static 
 void xen_pt_io_region_del(MemoryListener *l, MemoryRegionSection *sec)

XenPCIPassthroughState *s = container_of(l, XenPCIPassthroughState,
io_listener);

Quoted 9 lines

hw/xen/xen_pt.c static void xen_pt_region_del(MemoryListener *l, MemoryRegionSection *sec) · line 667

Quoted 6 lines
hw/xen/xen_pt.c +3 -0
+    if (!xen_pt_region_in_state(s, sec->mr)) {
+        return;
+    }
     xen_pt_region_update(s, sec, false);
     memory_region_unref(sec->mr);
 }

Wouldn't it make more sense to move the memory_region_ref/memory_region_unref calls inside xen_pt_region_update?

Move the operation of mr->ref inside xen_pt_region_update can also fix this bug. For the convenience of maintenance, another patch can be created.