Skip to contentSkip to conversation

Fix PCI passthrough for HVM with stubdomain

PATCH In review Marek Marczykowski-Górecki 5 participants 16 messages Oct 18, 2016 – Oct 25, 2016

lore
Share & tools
lore
#0 lore raw

cover Fix PCI passthrough for HVM with stubdomain

This series is follow up to previous attempts to fix this. Related threads:
- http://markmail.org/thread/dwjcdfk3y7s5c5kl "PCI passthrough with stubdomain"
- http://xen.markmail.org/thread/l7tvqcxbiyc2grvr "Json config and stubdomain"

With those patches applied, HVM finally have PCI devices working. Tested on Linux 4.4.14 and Window 7 pro guests (both 64bit).

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

#1 lore raw

PATCH 1/3 libxl: attach xen-pciback only to PV domains

HVM domains use IOMMU and device model assistance for communicating with PCI devices, xen-pcifront/pciback is used only in PV domains. When HVM domain has device model in stubdomain, attaching xen-pciback to the target domain itself is not only useless, but also may prevent attaching xen-pciback to the stubdomain, effectively breaking PCI passthrough.

---
 tools/libxl/libxl_pci.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/libxl/libxl_pci.c +3 -2
index 6f8f49c..2ae1bc4 100644
@@ -1111,7 +1111,7 @@ out:
         }
     }
 
-    if (!starting)
+    if (!starting && !hvm)
         rc = libxl__device_pci_add_xenstore(gc, domid, pcidev, starting);
     else
         rc = 0;
@@ -1306,7 +1306,8 @@ static void libxl__add_pcidevs(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
         }
     }
 
-    if (d_config->num_pcidevs > 0) {
+    if (d_config->num_pcidevs > 0
+            && d_config->c_info.type == LIBXL_DOMAIN_TYPE_PV) {
         rc = libxl__create_pci_backend(gc, domid, d_config->pcidevs,
             d_config->num_pcidevs);
         if (rc < 0) {
Signature · 7 lines
2.5.5


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

Re: [PATCH 1/3] libxl: attach xen-pciback only to PV domains

Quoted 6 lines · Marek Marczykowski-Górecki

HVM domains use IOMMU and device model assistance for communicating with PCI devices, xen-pcifront/pciback is used only in PV domains. When HVM domain has device model in stubdomain, attaching xen-pciback to the target domain itself is not only useless, but also may prevent attaching xen-pciback to the stubdomain, effectively breaking PCI passthrough.

This has the consequence that the "reset" of the device that pciback does will no longer be done.

That is the FLR functionality will not be exercised anymore.

Quoted 37 lines
---
 tools/libxl/libxl_pci.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/libxl/libxl_pci.c +3 -2
index 6f8f49c..2ae1bc4 100644
@@ -1111,7 +1111,7 @@ out:
         }
     }
 
-    if (!starting)
+    if (!starting && !hvm)
         rc = libxl__device_pci_add_xenstore(gc, domid, pcidev, starting);
     else
         rc = 0;
@@ -1306,7 +1306,8 @@ static void libxl__add_pcidevs(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
         }
     }
 
-    if (d_config->num_pcidevs > 0) {
+    if (d_config->num_pcidevs > 0
+            && d_config->c_info.type == LIBXL_DOMAIN_TYPE_PV) {
         rc = libxl__create_pci_backend(gc, domid, d_config->pcidevs,
             d_config->num_pcidevs);
         if (rc < 0) {
Signature · 7 lines
2.5.5


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

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

#3 lore raw

Re: [PATCH 1/3] libxl: attach xen-pciback only to PV domains

Quoted 8 lines · Konrad Rzeszutek Wilk
Quoted 6 lines · Marek Marczykowski-Górecki

HVM domains use IOMMU and device model assistance for communicating with PCI devices, xen-pcifront/pciback is used only in PV domains. When HVM domain has device model in stubdomain, attaching xen-pciback to the target domain itself is not only useless, but also may prevent attaching xen-pciback to the stubdomain, effectively breaking PCI passthrough.

Quoted 4 lines

This has the consequence that the "reset" of the device that pciback does will no longer be done.

That is the FLR functionality will not be exercised anymore.

Are you sure about that? libxl__device_pci_add calls libxl__device_pci_reset, regardless of my patch.

Quoted 36 lines
Quoted 36 lines
---
 tools/libxl/libxl_pci.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/libxl/libxl_pci.c +3 -2
index 6f8f49c..2ae1bc4 100644
@@ -1111,7 +1111,7 @@ out:
         }
     }
 
-    if (!starting)
+    if (!starting && !hvm)
         rc = libxl__device_pci_add_xenstore(gc, domid, pcidev, starting);
     else
         rc = 0;
@@ -1306,7 +1306,8 @@ static void libxl__add_pcidevs(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
         }
     }
 
-    if (d_config->num_pcidevs > 0) {
+    if (d_config->num_pcidevs > 0
+            && d_config->c_info.type == LIBXL_DOMAIN_TYPE_PV) {
         rc = libxl__create_pci_backend(gc, domid, d_config->pcidevs,
             d_config->num_pcidevs);
         if (rc < 0) {
Signature · 7 lines
2.5.5


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Signature · 5 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

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

#4 lore raw

Re: [PATCH 1/3] libxl: attach xen-pciback only to PV domains

In reply to Marek Marczykowski-Górecki
Quoted 2 lines · Marek Marczykowski-Górecki

HVM domains use IOMMU and device model assistance for communicating with PCI devices, xen-pcifront/pciback is used only in PV domains.

This bit of description is in line with my understanding of how PCI passthrough works.

Quoted 24 lines

When HVM domain has device model in stubdomain, attaching xen-pciback to the target domain itself is not only useless, but also may prevent attaching xen-pciback to the stubdomain, effectively breaking PCI passthrough.

---
 tools/libxl/libxl_pci.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/libxl/libxl_pci.c +1 -1
index 6f8f49c..2ae1bc4 100644
@@ -1111,7 +1111,7 @@ out:
         }
     }
 
-    if (!starting)
+    if (!starting && !hvm)
         rc = libxl__device_pci_add_xenstore(gc, domid, pcidev, starting);
     else
         rc = 0;

tools/libxl/libxl_pci.c static void libxl__add_pcidevs(libxl__egc *egc, libxl__ao *ao, uint32_t domid, · line 1310

Quoted 6 lines
tools/libxl/libxl_pci.c +2 -1
@@ -1306,7 +1306,8 @@ static void libxl__add_pcidevs(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
         }
     }
 
-    if (d_config->num_pcidevs > 0) {
+    if (d_config->num_pcidevs > 0
+            && d_config->c_info.type == LIBXL_DOMAIN_TYPE_PV) {

Please move the indentation forward.

Quoted 6 lines
tools/libxl/libxl_pci.c +0 -0
         rc = libxl__create_pci_backend(gc, domid, d_config->pcidevs,
             d_config->num_pcidevs);
         if (rc < 0) {
Signature · 1 line
2.5.5

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

#5 lore raw

Re: [PATCH 1/3] libxl: attach xen-pciback only to PV domains

Quoted 6 lines · Wei Liu
Quoted 2 lines · Marek Marczykowski-Górecki

HVM domains use IOMMU and device model assistance for communicating with PCI devices, xen-pcifront/pciback is used only in PV domains.

This bit of description is in line with my understanding of how PCI passthrough works.

Kind of. Pciback is also used to "own" the PCI devices. And in fact they do an important job of resetting the PCI device when the device is "bind" to pciback:

echo <Bdf> > bind

And .. this is the important part - when device changes ownership. That is when you disconnect it from one guest and assign to another. You need to reset the device in between. The code that calls the pci_reset_function is called by:

}

/*
* Called when:

 *  - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
 *  - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove
 *  - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
 *  - 'echo BDF > unbind' with a guest still using it. See pcistub_remove       

*
* As such we have to be careful.
*
* To make this easier, the caller has to hold the device lock. */
void pcistub_put_pci_dev(struct pci_dev *dev)

The first two are done when XenStore 'pci' entries are active - which this patch will remove and introduce a potential security problem.

Unless libxl does an 'unbind' followed by an 'bind'?

Quoted 41 lines
Quoted 25 lines

When HVM domain has device model in stubdomain, attaching xen-pciback to the target domain itself is not only useless, but also may prevent attaching xen-pciback to the stubdomain, effectively breaking PCI passthrough.

---
 tools/libxl/libxl_pci.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/libxl/libxl_pci.c +1 -1
index 6f8f49c..2ae1bc4 100644
@@ -1111,7 +1111,7 @@ out:
         }
     }
 
-    if (!starting)
+    if (!starting && !hvm)
         rc = libxl__device_pci_add_xenstore(gc, domid, pcidev, starting);
     else
         rc = 0;

tools/libxl/libxl_pci.c static void libxl__add_pcidevs(libxl__egc *egc, libxl__ao *ao, uint32_t domid, · line 1310

Quoted 6 lines
tools/libxl/libxl_pci.c +2 -1
@@ -1306,7 +1306,8 @@ static void libxl__add_pcidevs(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
         }
     }
 
-    if (d_config->num_pcidevs > 0) {
+    if (d_config->num_pcidevs > 0
+            && d_config->c_info.type == LIBXL_DOMAIN_TYPE_PV) {

Please move the indentation forward.

Quoted 6 lines
tools/libxl/libxl_pci.c +0 -0
         rc = libxl__create_pci_backend(gc, domid, d_config->pcidevs,
             d_config->num_pcidevs);
         if (rc < 0) {
Signature · 1 line
2.5.5
Quoted 4 lines

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

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

#6 lore raw

Re: [PATCH 1/3] libxl: attach xen-pciback only to PV domains

Quoted 5 lines · Konrad Rzeszutek Wilk
Quoted 4 lines · Wei Liu
Quoted 2 lines · Marek Marczykowski-Górecki

HVM domains use IOMMU and device model assistance for communicating with PCI devices, xen-pcifront/pciback is used only in PV domains.

Quoted 8 lines
Quoted 2 lines

This bit of description is in line with my understanding of how PCI passthrough works.

Kind of. Pciback is also used to "own" the PCI devices. And in fact they do an important job of resetting the PCI device when the device is "bind" to pciback:

echo <Bdf> > bind

This part is still done.

Quoted 11 lines

And .. this is the important part - when device changes ownership. That is when you disconnect it from one guest and assign to another. You need to reset the device in between. The code that calls the pci_reset_function is called by:

}

/*
* Called when:
* - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
* - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove

But this, in case of HVM without stubdomain, is not.

Quoted 9 lines

* - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
* - 'echo BDF > unbind' with a guest still using it. See pcistub_remove *
* As such we have to be careful.
*
* To make this easier, the caller has to hold the device lock.
*/
void pcistub_put_pci_dev(struct pci_dev *dev)

Quoted 4 lines

The first two are done when XenStore 'pci' entries are active - which this patch will remove and introduce a potential security problem.

Unless libxl does an 'unbind' followed by an 'bind'?

What about libxl__device_pci_reset, which is called (at least) before attaching device to some domain, even after my patch and even if the device is already bound to pciback. It tries to reset the device using 'reset' entry in sysfs. I see this isn't available for some devices - can pci_reset_function do any better?

Quoted 46 lines
Quoted 46 lines
Quoted 26 lines

When HVM domain has device model in stubdomain, attaching xen-pciback to the target domain itself is not only useless, but also may prevent attaching xen-pciback to the stubdomain, effectively breaking PCI passthrough.

---
 tools/libxl/libxl_pci.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/libxl/libxl_pci.c +1 -1
index 6f8f49c..2ae1bc4 100644
@@ -1111,7 +1111,7 @@ out:
         }
     }
 
-    if (!starting)
+    if (!starting && !hvm)
         rc = libxl__device_pci_add_xenstore(gc, domid, pcidev, starting);
     else
         rc = 0;

tools/libxl/libxl_pci.c static void libxl__add_pcidevs(libxl__egc *egc, libxl__ao *ao, uint32_t domid, · line 1310

Quoted 6 lines
tools/libxl/libxl_pci.c +2 -1
@@ -1306,7 +1306,8 @@ static void libxl__add_pcidevs(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
         }
     }
 
-    if (d_config->num_pcidevs > 0) {
+    if (d_config->num_pcidevs > 0
+            && d_config->c_info.type == LIBXL_DOMAIN_TYPE_PV) {

Please move the indentation forward.

Quoted 6 lines
tools/libxl/libxl_pci.c +0 -0
         rc = libxl__create_pci_backend(gc, domid, d_config->pcidevs,
             d_config->num_pcidevs);
         if (rc < 0) {
Signature · 1 line
2.5.5

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

Signature · 5 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

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

#7 lore raw

Re: [PATCH 1/3] libxl: attach xen-pciback only to PV domains

Quoted 43 lines · Marek Marczykowski-Górecki
Quoted 5 lines · Konrad Rzeszutek Wilk
Quoted 4 lines · Wei Liu
Quoted 2 lines · Marek Marczykowski-Górecki

HVM domains use IOMMU and device model assistance for communicating with PCI devices, xen-pcifront/pciback is used only in PV domains.

Quoted 8 lines
Quoted 2 lines

This bit of description is in line with my understanding of how PCI passthrough works.

Kind of. Pciback is also used to "own" the PCI devices. And in fact they do an important job of resetting the PCI device when the device is "bind" to pciback:

echo <Bdf> > bind

This part is still done.

Quoted 11 lines

And .. this is the important part - when device changes ownership. That is when you disconnect it from one guest and assign to another. You need to reset the device in between. The code that calls the pci_reset_function is called by:

}

/*
* Called when:
* - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
* - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove

But this, in case of HVM without stubdomain, is not.

Quoted 12 lines

* - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
* - 'echo BDF > unbind' with a guest still using it. See pcistub_remove *
* As such we have to be careful.
*
* To make this easier, the caller has to hold the device lock.
*/
void pcistub_put_pci_dev(struct pci_dev *dev)

The first two are done when XenStore 'pci' entries are active - which this patch will remove and introduce a potential security problem.

Quoted 7 lines
Quoted 1 line

Unless libxl does an 'unbind' followed by an 'bind'?

What about libxl__device_pci_reset, which is called (at least) before attaching device to some domain, even after my patch and even if the device is already bound to pciback. It tries to reset the device using 'reset' entry in sysfs. I see this isn't available for some devices - can pci_reset_function do any better?

My vague recollection was that it tried to do it but it aborted earlier due to holding locks (dev_lock is held when you do any operation on the SysFS). But I may be forgetting the details.

I need to look in the Linux code to confirm what the tricky part was.

Quoted 44 lines
Quoted 44 lines
Quoted 44 lines
Quoted 28 lines

When HVM domain has device model in stubdomain, attaching xen-pciback to the target domain itself is not only useless, but also may prevent attaching xen-pciback to the stubdomain, effectively breaking PCI passthrough.

---
 tools/libxl/libxl_pci.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/libxl/libxl_pci.c +1 -1
index 6f8f49c..2ae1bc4 100644
@@ -1111,7 +1111,7 @@ out:
         }
     }
 
-    if (!starting)
+    if (!starting && !hvm)
         rc = libxl__device_pci_add_xenstore(gc, domid, pcidev, starting);
     else
         rc = 0;

tools/libxl/libxl_pci.c static void libxl__add_pcidevs(libxl__egc *egc, libxl__ao *ao, uint32_t domid, · line 1310

Quoted 6 lines
tools/libxl/libxl_pci.c +2 -1
@@ -1306,7 +1306,8 @@ static void libxl__add_pcidevs(libxl__egc *egc, libxl__ao *ao, uint32_t domid,
         }
     }
 
-    if (d_config->num_pcidevs > 0) {
+    if (d_config->num_pcidevs > 0
+            && d_config->c_info.type == LIBXL_DOMAIN_TYPE_PV) {

Please move the indentation forward.

Quoted 6 lines
tools/libxl/libxl_pci.c +0 -0
         rc = libxl__create_pci_backend(gc, domid, d_config->pcidevs,
             d_config->num_pcidevs);
         if (rc < 0) {
Signature · 1 line
2.5.5
Quoted 11 lines
Quoted 4 lines
Quoted 4 lines

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

Signature · 5 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

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

#8 lore raw

Re: [PATCH 1/3] libxl: attach xen-pciback only to PV domains

Quoted 52 lines · Konrad Rzeszutek Wilk
Quoted 43 lines · Marek Marczykowski-Górecki
Quoted 5 lines · Konrad Rzeszutek Wilk
Quoted 4 lines · Wei Liu
Quoted 2 lines · Marek Marczykowski-Górecki

HVM domains use IOMMU and device model assistance for communicating with PCI devices, xen-pcifront/pciback is used only in PV domains.

Quoted 8 lines
Quoted 2 lines

This bit of description is in line with my understanding of how PCI passthrough works.

Kind of. Pciback is also used to "own" the PCI devices. And in fact they do an important job of resetting the PCI device when the device is "bind" to pciback:

echo <Bdf> > bind

This part is still done.

Quoted 11 lines

And .. this is the important part - when device changes ownership. That is when you disconnect it from one guest and assign to another. You need to reset the device in between. The code that calls the pci_reset_function is called by:

}

/*
* Called when:
* - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
* - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove

But this, in case of HVM without stubdomain, is not.

Quoted 12 lines

* - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
* - 'echo BDF > unbind' with a guest still using it. See pcistub_remove *
* As such we have to be careful.
*
* To make this easier, the caller has to hold the device lock.
*/
void pcistub_put_pci_dev(struct pci_dev *dev)

The first two are done when XenStore 'pci' entries are active - which this patch will remove and introduce a potential security problem.

Quoted 7 lines
Quoted 1 line

Unless libxl does an 'unbind' followed by an 'bind'?

What about libxl__device_pci_reset, which is called (at least) before attaching device to some domain, even after my patch and even if the device is already bound to pciback. It tries to reset the device using 'reset' entry in sysfs. I see this isn't available for some devices - can pci_reset_function do any better?

Quoted 5 lines

My vague recollection was that it tried to do it but it aborted earlier due to holding locks (dev_lock is held when you do any operation on the SysFS). But I may be forgetting the details.

I need to look in the Linux code to confirm what the tricky part was.

Thanks. This is the last thing holding me from sending v2.

Anyway, if attaching xen-pciback to /something/ is needed, how should it look? We have 3 cases:
1. PV - without qemu
2. HVM - with qemu in dom0
3. HVM - with qemu in stubdomain
And soon there will be 4th: PVH - without qemu

For 1 and 4 the device should be attached (in terms of xenstore) to the target domain, as xen-pcifront (or equivalent) running there will be used. BTW is that true for PVHv2?
For 3 - it should be attached to stubdomain (which is the case). The question is what about 2 - should it be attached to the target domain, even though it will not be used?

Signature · 5 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

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

#9 lore raw

Re: [PATCH 1/3] libxl: attach xen-pciback only to PV domains

Quoted 61 lines · Marek Marczykowski-Górecki
Quoted 46 lines · Konrad Rzeszutek Wilk
Quoted 39 lines · Marek Marczykowski-Górecki
Quoted 11 lines · Konrad Rzeszutek Wilk
Quoted 5 lines · Wei Liu
Quoted 2 lines · Marek Marczykowski-Górecki

HVM domains use IOMMU and device model assistance for communicating with PCI devices, xen-pcifront/pciback is used only in PV domains.

This bit of description is in line with my understanding of how PCI passthrough works.

Kind of. Pciback is also used to "own" the PCI devices. And in fact they do an important job of resetting the PCI device when the device is "bind" to pciback:

echo <Bdf> > bind

This part is still done.

Quoted 11 lines

And .. this is the important part - when device changes ownership. That is when you disconnect it from one guest and assign to another. You need to reset the device in between. The code that calls the pci_reset_function is called by:

}

/*
* Called when:
* - XenBus state has been reconfigure (pci unplug). See xen_pcibk_remove_device
* - XenBus state has been disconnected (guest shutdown). See xen_pcibk_xenbus_remove

But this, in case of HVM without stubdomain, is not.

Quoted 12 lines

* - 'echo BDF > unbind' on pciback module with no guest attached. See pcistub_remove
* - 'echo BDF > unbind' with a guest still using it. See pcistub_remove *
* As such we have to be careful.
*
* To make this easier, the caller has to hold the device lock.
*/
void pcistub_put_pci_dev(struct pci_dev *dev)

The first two are done when XenStore 'pci' entries are active - which this patch will remove and introduce a potential security problem.

Quoted 6 lines
Quoted 1 line

Unless libxl does an 'unbind' followed by an 'bind'?

What about libxl__device_pci_reset, which is called (at least) before attaching device to some domain, even after my patch and even if the device is already bound to pciback. It tries to reset the device using 'reset' entry in sysfs. I see this isn't available for some devices - can pci_reset_function do any better?

Quoted 5 lines

My vague recollection was that it tried to do it but it aborted earlier due to holding locks (dev_lock is held when you do any operation on the SysFS). But I may be forgetting the details.

I need to look in the Linux code to confirm what the tricky part was.

Thanks. This is the last thing holding me from sending v2.

Anyway, if attaching xen-pciback to /something/ is needed, how should it look? We have 3 cases:
1. PV - without qemu
2. HVM - with qemu in dom0
3. HVM - with qemu in stubdomain
And soon there will be 4th: PVH - without qemu

Quoted 6 lines

For 1 and 4 the device should be attached (in terms of xenstore) to the target domain, as xen-pcifront (or equivalent) running there will be used. BTW is that true for PVHv2?
For 3 - it should be attached to stubdomain (which is the case). The question is what about 2 - should it be attached to the target domain, even though it will not be used?

PVH(v2) is a little complicated. For dom0 support, there are some bits of basic bridge emulation moving into the hypervisor so qemu isn't required at all. In practice, this means that SRIOV passthrough to plain PVH(v2) domU's will also work without qemu.

There are specific plans to not use pcifront in PVH(v2) guests, making it closer to how real hardware works. If however there are complicated bits of faking up required (e.g. Graphics IO-bars so windows doesn't refuse to load the driver), then that will be better relegated to a very small ioreq server driver in dom0, similar to how demu currently works.

As for the other cases. The one and only legitimate case where a guest can find any information about its pci devices in xenstore is PV guests. HVM guests must under no circumstance be in a position to use pci-front. Both pci-back and qemu have their own model of PCI state, and an HVM guest must not be able to do thinks like half an update via one method and another half via the other.

It is unfortunate that xen-pciback has dual unrelated functionality. The "binding to arbitrary devices" should be split out into a separate device, leaving xen-pciback as only the back half of the shared protocol.

~Andrew

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

#10 lore raw

PATCH 2/3 libxl: attach PCI device to qemu only after setting pciback/pcifront

In reply to Marek Marczykowski-Górecki

When qemu is running in stubdomain, handling "pci-ins" command will fail if pcifront is not initialized already. Fix this by sending such command only after confirming that pciback/front is running.

It is possible to handle this case in mini-os code, by delaying "pci-ins" handling until pcifront_thread finishes devices discovery. But the same problem most likely will apply to any other stubdomain implementations when they come (Rumprun, Linux) - so better handle this at libxl level.

---
 tools/libxl/libxl_pci.c | 9 +++++++++
 1 file changed, 9 insertions(+)
tools/libxl/libxl_pci.c +9 -0
index 2ae1bc4..3805d30 100644
@@ -1195,6 +1195,7 @@ int libxl__device_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcide
 {
     libxl_ctx *ctx = libxl__gc_owner(gc);
     unsigned int orig_vdev, pfunc_mask;
+    char *be_path;
     libxl_device_pci *assigned;
     int num_assigned, i, rc;
     int stubdomid = 0;
@@ -1249,6 +1250,14 @@ int libxl__device_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcide
         rc = do_pci_add(gc, stubdomid, &pcidev_s, 0);
         if ( rc )
             goto out;
+        /* wait for the device actually being connected, otherwise device model
+         * running there will fail to find the device */
+        be_path = libxl__sprintf(gc, "%s/backend/pci/%d/0",
+                libxl__xs_get_dompath(gc, 0), stubdomid);
+        rc = libxl__wait_for_backend(gc, be_path,
+                    GCSPRINTF("%d", XenbusStateConnected));
+        if ( rc )
+            goto out;
     }
 
     orig_vdev = pcidev->vdevfn & ~7U;
Signature · 7 lines
2.5.5


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

Re: [PATCH 2/3] libxl: attach PCI device to qemu only after setting pciback/pcifront

Quoted 11 lines · Marek Marczykowski-Górecki

When qemu is running in stubdomain, handling "pci-ins" command will fail if pcifront is not initialized already. Fix this by sending such command only after confirming that pciback/front is running.

It is possible to handle this case in mini-os code, by delaying "pci-ins" handling until pcifront_thread finishes devices discovery. But the same problem most likely will apply to any other stubdomain implementations when they come (Rumprun, Linux) - so better handle this at libxl level.

Mostly some coding style issues.

Quoted 17 lines
---
 tools/libxl/libxl_pci.c | 9 +++++++++
 1 file changed, 9 insertions(+)
tools/libxl/libxl_pci.c +1 -0
index 2ae1bc4..3805d30 100644
@@ -1195,6 +1195,7 @@ int libxl__device_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcide
 {
     libxl_ctx *ctx = libxl__gc_owner(gc);
     unsigned int orig_vdev, pfunc_mask;
+    char *be_path;
     libxl_device_pci *assigned;
     int num_assigned, i, rc;
     int stubdomid = 0;

tools/libxl/libxl_pci.c int libxl__device_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcide · line 1254

Quoted 5 lines
tools/libxl/libxl_pci.c +2 -0
@@ -1249,6 +1250,14 @@ int libxl__device_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcide
         rc = do_pci_add(gc, stubdomid, &pcidev_s, 0);
         if ( rc )
             goto out;
+        /* wait for the device actually being connected, otherwise device model
+         * running there will fail to find the device */

Hmm... I don't think we have written down style requirement for code comment, but I would like this comment to be formatted a bit.

/* Wait for the device actually being connected, otherwise device model
* running there will fail to find the device. */

That is to capitalise the first word and make it a proper sentence.

tools/libxl/libxl_pci.c int libxl__device_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcide · line 1258

Quoted 4 lines
tools/libxl/libxl_pci.c +4 -0
+        be_path = libxl__sprintf(gc, "%s/backend/pci/%d/0",
+                libxl__xs_get_dompath(gc, 0), stubdomid);
+        rc = libxl__wait_for_backend(gc, be_path,
+                    GCSPRINTF("%d", XenbusStateConnected));

Please indent this a bit.

tools/libxl/libxl_pci.c int libxl__device_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcide · line 1259

Quoted 1 line
tools/libxl/libxl_pci.c +1 -0
+        if ( rc )

Not really your problem. This file is ridden with such style violation. Can you please remove the space before and after "rc"?

Wei.

Quoted 7 lines
tools/libxl/libxl_pci.c +1 -0
+            goto out;
     }
 
     orig_vdev = pcidev->vdevfn & ~7U;
Signature · 1 line
2.5.5

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

#12 lore raw

PATCH 3/3 libxl: don't try to manipulate json config for stubdomain

In reply to Marek Marczykowski-Górecki

Stubdomain do not have it's own config file - its configuration is derived from target domains. Do not try to manipulate it when attaching PCI device.

This bug prevented starting HVM with stubdomain and PCI passthrough device attached.

---
 tools/libxl/libxl_pci.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
tools/libxl/libxl_pci.c +15 -9
index 3805d30..5ad70c5 100644
@@ -151,14 +151,18 @@ static int libxl__device_pci_add_xenstore(libxl__gc *gc, uint32_t domid, libxl_d
     GCNEW(device);
     libxl__device_from_pcidev(gc, domid, pcidev, device);
 
-    lock = libxl__lock_domain_userdata(gc, domid);
-    if (!lock) {
-        rc = ERROR_LOCK_FAIL;
-        goto out;
-    }
+    /* Stubdomain config is derived from its target domain, it doesn't have
+       its own file */
+    if (!libxl_is_stubdom(CTX, domid, NULL)) {
+        lock = libxl__lock_domain_userdata(gc, domid);
+        if (!lock) {
+            rc = ERROR_LOCK_FAIL;
+            goto out;
+        }
 
-    rc = libxl__get_domain_configuration(gc, domid, &d_config);
-    if (rc) goto out;
+        rc = libxl__get_domain_configuration(gc, domid, &d_config);
+        if (rc) goto out;
+    }
 
     DEVICE_ADD(pci, pcidevs, domid, &pcidev_saved, COMPARE_PCI, &d_config);
 
@@ -169,8 +173,10 @@ static int libxl__device_pci_add_xenstore(libxl__gc *gc, uint32_t domid, libxl_d
         rc = libxl__xs_transaction_start(gc, &t);
         if (rc) goto out;
 
-        rc = libxl__set_domain_configuration(gc, domid, &d_config);
-        if (rc) goto out;
+        if (lock) {
+            rc = libxl__set_domain_configuration(gc, domid, &d_config);
+            if (rc) goto out;
+        }
 
         libxl__xs_writev(gc, t, be_path, libxl__xs_kvs_of_flexarray(gc, back));
 
Signature · 7 lines
2.5.5


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

Re: [PATCH 3/3] libxl: don't try to manipulate json config for stubdomain

Quoted 24 lines · Marek Marczykowski-Górecki

Stubdomain do not have it's own config file - its configuration is derived from target domains. Do not try to manipulate it when attaching PCI device.

This bug prevented starting HVM with stubdomain and PCI passthrough device attached.

---
 tools/libxl/libxl_pci.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
tools/libxl/libxl_pci.c +0 -4
index 3805d30..5ad70c5 100644
@@ -151,14 +151,18 @@ static int libxl__device_pci_add_xenstore(libxl__gc *gc, uint32_t domid, libxl_d
     GCNEW(device);
     libxl__device_from_pcidev(gc, domid, pcidev, device);
 
-    lock = libxl__lock_domain_userdata(gc, domid);
-    if (!lock) {
-        rc = ERROR_LOCK_FAIL;
-        goto out;

tools/libxl/libxl_pci.c static int libxl__device_pci_add_xenstore(libxl__gc *gc, uint32_t domid, libxl_d · line 162

Quoted 10 lines
tools/libxl/libxl_pci.c +8 -1
-    }
+    /* Stubdomain config is derived from its target domain, it doesn't have
+       its own file */
+    if (!libxl_is_stubdom(CTX, domid, NULL)) {
+        lock = libxl__lock_domain_userdata(gc, domid);
+        if (!lock) {
+            rc = ERROR_LOCK_FAIL;
+            goto out;
+        }
 

What makes PCI devices special with regard to other devices? In other words, do we need to make similar changes to other devices as well?

Wei.

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

#14 lore raw

Re: [PATCH 3/3] libxl: don't try to manipulate json config for stubdomain

Quoted 30 lines · Wei Liu
Quoted 29 lines · Marek Marczykowski-Górecki

Stubdomain do not have it's own config file - its configuration is derived from target domains. Do not try to manipulate it when attaching PCI device.

This bug prevented starting HVM with stubdomain and PCI passthrough device attached.

---
 tools/libxl/libxl_pci.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
tools/libxl/libxl_pci.c +4 -5
index 3805d30..5ad70c5 100644
@@ -151,14 +151,18 @@ static int libxl__device_pci_add_xenstore(libxl__gc *gc, uint32_t domid, libxl_d
     GCNEW(device);
     libxl__device_from_pcidev(gc, domid, pcidev, device);
 
-    lock = libxl__lock_domain_userdata(gc, domid);
-    if (!lock) {
-        rc = ERROR_LOCK_FAIL;
-        goto out;
-    }
+    /* Stubdomain config is derived from its target domain, it doesn't have
+       its own file */
+    if (!libxl_is_stubdom(CTX, domid, NULL)) {
+        lock = libxl__lock_domain_userdata(gc, domid);
Quoted 8 lines

tools/libxl/libxl_pci.c static int libxl__device_pci_add_xenstore(libxl__gc *gc, uint32_t domid, libxl_d · line 162

Quoted 5 lines
tools/libxl/libxl_pci.c +4 -0
+        if (!lock) {
+            rc = ERROR_LOCK_FAIL;
+            goto out;
+        }
 

What makes PCI devices special with regard to other devices? In other words, do we need to make similar changes to other devices as well?

I think PCI is special because libxl doesn't bother to update stubdomain configuration for other device types at all... If I try dynamic attach of network device to a HVM (with stubdomain), it gets attached _only_ to the target domain. So HVM without any PV drivers is out of luck. Is it supposed to work with qemu in dom0?

Signature · 5 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

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

#15 lore raw
In reply to Marek Marczykowski-Górecki

Hello,

Quoted 6 lines · Marek Marczykowski-Górecki, on Tue 18 Oct 2016 03:53:30 +0200, wrote:

This series is follow up to previous attempts to fix this. Related threads:
- http://markmail.org/thread/dwjcdfk3y7s5c5kl "PCI passthrough with stubdomain"
- http://xen.markmail.org/thread/l7tvqcxbiyc2grvr "Json config and stubdomain"

With those patches applied, HVM finally have PCI devices working. Tested on Linux 4.4.14 and Window 7 pro guests (both 64bit).

The three changes make sense to me. I don't know libxl any more, so I wouldn't be able to review them, though :)

Thanks!
Samuel

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