Skip to contentSkip to conversation

Support device passthrough when dom0 is PVH on Xen

RFC PATCH Superseded Jiqian Chen 4 participants 21 messages Jan 12, 2024 – Feb 26, 2024

v5 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v5 cover Support device passthrough when dom0 is PVH on Xen

Hi All,
This is v5 series to support passthrough when dom0 is PVH
v4->v5 changes:
* patch#1: add pci_lock wrap function vpci_reset_device_state
* patch#2: move the check of self map_pirq to physdev.c, and change to check if the caller has PIRQ flag, and just break for PHYSDEVOP_(un)map_pirq in hvm_physdev_op
* patch#3: return -EOPNOTSUPP instead, and use ASSERT(!has_pirq(currd));
* patch#4: is the patch#5 in v4 because patch#5 in v5 has some dependency on it. And add the handling of errno and add the Reviewed-by Stefano
* patch#5: is the patch#4 in v4. New implementation to add new hypercall XEN_DOMCTL_gsi_permission to grant gsi

v3->v4 changes:
* patch#1: change the comment of PHYSDEVOP_pci_device_state_reset; move printings behind pcidevs_unlock
* patch#2: add check to prevent PVH self map
* patch#3: new patch, The implementation of adding PHYSDEVOP_setup_gsi for PVH is treated as a separate patch
* patch#4: new patch to solve the map_pirq problem of PVH dom0. use gsi to grant irq permission in XEN_DOMCTL_irq_permission.
* patch#5: to be compatible with previous kernel versions, when there is no gsi sysfs, still use irq
v4 link:
https://lore.kernel.org/xen-devel/[email protected]/T/#t

v2->v3 changes:
* patch#1: move the content out of pci_reset_device_state and delete pci_reset_device_state; add xsm_resource_setup_pci check for PHYSDEVOP_pci_device_state_reset; add description for PHYSDEVOP_pci_device_state_reset;
* patch#2: du to changes in the implementation of the second patch on kernel side(that it will do setup_gsi and map_pirq when assigning a device to passthrough), add PHYSDEVOP_setup_gsi for PVH dom0, and we need to support self mapping.
* patch#3: du to changes in the implementation of the second patch on kernel side(that adds a new sysfs for gsi instead of a new syscall), so read gsi number from the sysfs of gsi.
v3 link:
https://lore.kernel.org/xen-devel/[email protected]/T/#t

v2 link:
https://lore.kernel.org/xen-devel/[email protected]/T/#t
Below is the description of v2 cover letter:
This series of patches are the v2 of the implementation of passthrough when dom0 is PVH on Xen. We sent the v1 to upstream before, but the v1 had so many problems and we got lots of suggestions. I will introduce all issues that these patches try to fix and the differences between v1 and v2.

Issues we encountered:
1. pci_stub failed to write bar for a passthrough device.
Problem: when we run “sudo xl pci-assignable-add <sbdf>” to assign a device, pci_stub will call “pcistub_init_device() -> pci_restore_state() -> pci_restore_config_space() -> pci_restore_config_space_range() -> pci_restore_config_dword() -> pci_write_config_dword()”, the pci config write will trigger an io interrupt to bar_write() in the xen, but the bar->enabled was set before, the write is not allowed now, and then when
bar->Qemu config the
passthrough device in xen_pt_realize(), it gets invalid bar values.

Reason: the reason is that we don't tell vPCI that the device has been reset, so the current cached state in pdev->vpci is all out of date and is different from the real device state.

Solution: to solve this problem, the first patch of kernel(xen/pci: Add xen_reset_device_state
function) and the fist patch of xen(xen/vpci: Clear all vpci status of device) add a new hypercall to reset the state stored in vPCI when the state of real device has changed.
Thank Roger for the suggestion of this v2, and it is different from v1 (https://lore.kernel.org/xen-devel/[email protected]/), v1 simply allow domU to write pci bar, it does not comply with the design principles of vPCI.

2. failed to do PHYSDEVOP_map_pirq when dom0 is PVH
Problem: HVM domU will do PHYSDEVOP_map_pirq for a passthrough device by using gsi. See xen_pt_realize->xc_physdev_map_pirq and pci_add_dm_done->xc_physdev_map_pirq. Then xc_physdev_map_pirq will call into Xen, but in hvm_physdev_op(), PHYSDEVOP_map_pirq is not allowed.

Reason: In hvm_physdev_op(), the variable "currd" is PVH dom0 and PVH has no X86_EMU_USE_PIRQ flag, it will fail at has_pirq check.

Solution: I think we may need to allow PHYSDEVOP_map_pirq when "currd" is dom0 (at present dom0 is PVH). The second patch of xen(x86/pvh: Open PHYSDEVOP_map_pirq for PVH dom0) allow PVH dom0 do PHYSDEVOP_map_pirq. This v2 patch is better than v1, v1 simply remove the has_pirq check(xen https://lore.kernel.org/xen-devel/[email protected]/).

3. the gsi of a passthrough device doesn't be unmasked 3.1 failed to check the permission of pirq
3.2 the gsi of passthrough device was not registered in PVH dom0

Problem:
3.1 callback function pci_add_dm_done() will be called when qemu config a passthrough device for domU.
This function will call xc_domain_irq_permission()-> pirq_access_permitted() to check if the gsi has corresponding mappings in dom0. But it didn’t, so failed. See XEN_DOMCTL_irq_permission->pirq_access_permitted, "current" is PVH dom0 and it return irq is 0. 3.2 it's possible for a gsi (iow: vIO-APIC pin) to never get registered on PVH dom0, because the devices of PVH are using MSI(-X) interrupts. However, the IO-APIC pin must be configured for it to be able to be mapped into a domU.

Reason: After searching codes, I find "map_pirq" and "register_gsi" will be done in function vioapic_write_redirent->vioapic_hwdom_map_gsi when the gsi(aka ioapic's pin) is unmasked in PVH dom0. So the two problems can be concluded to that the gsi of a passthrough device doesn't be unmasked.

Solution: to solve these problems, the second patch of kernel(xen/pvh: Unmask irq for passthrough device in PVH dom0) call the unmask_irq() when we assign a device to be passthrough. So that passthrough devices can have the mapping of gsi on PVH dom0 and gsi can be registered. This v2 patch is different from the v1( kernel https://lore.kernel.org/xen-devel/[email protected]/,
kernel https://lore.kernel.org/xen-devel/[email protected]/ and xen https://lore.kernel.org/xen-devel/[email protected]/),
v1 performed "map_pirq" and "register_gsi" on all pci devices on PVH dom0, which is unnecessary and may cause multiple registration.

4. failed to map pirq for gsi
Problem: qemu will call xc_physdev_map_pirq() to map a passthrough device’s gsi to pirq in function xen_pt_realize(). But failed.

Reason: According to the implement of xc_physdev_map_pirq(), it needs gsi instead of irq, but qemu pass irq to it and treat irq as gsi, it is got from file /sys/bus/pci/devices/xxxx:xx:xx.x/irq in function xen_host_pci_device_get(). But actually the gsi number is not equal with irq. On PVH dom0, when it allocates irq for a gsi in function acpi_register_gsi_ioapic(), allocation is dynamic, and follow the principle of applying first, distributing first. And if you debug the kernel codes(see function __irq_alloc_descs), you will find the irq number is allocated from small to large by order, but the applying gsi number is not, gsi 38 may come before gsi 28, that causes gsi 38 get a smaller irq number than gsi 28, and then gsi != irq.

Solution: we can record the relation between gsi and irq, then when userspace(qemu) want to use gsi, we can do a translation. The third patch of kernel(xen/privcmd: Add new syscall to get gsi from irq) records all the relations in acpi_register_gsi_xen_pvh() when dom0 initialize pci devices, and provide a syscall for userspace to get the gsi from irq. The third patch of xen(tools: Add new function to get gsi from irq) add a new function xc_physdev_gsi_from_irq() to call the new syscall added on kernel side. And then userspace can use that function to get gsi. Then xc_physdev_map_pirq() will success. This v2 patch is the same as v1( kernel https://lore.kernel.org/xen-devel/[email protected]/ and xen https://lore.kernel.org/xen-devel/[email protected]/)

About the v2 patch of qemu, just change an included head file, other are similar to the v1 ( qemu https://lore.kernel.org/xen-devel/[email protected]/), just call xc_physdev_gsi_from_irq() to get gsi from irq.

Jiqian Chen (5):
xen/vpci: Clear all vpci status of device x86/pvh: Allow (un)map_pirq when dom0 is PVH x86/pvh: Add PHYSDEVOP_setup_gsi for PVH dom0
libxl: Use gsi instead of irq for mapping pirq
domctl: Add XEN_DOMCTL_gsi_permission to grant gsi

 tools/include/xenctrl.h      |  5 +++++
 tools/libs/ctrl/xc_domain.c  | 15 +++++++++++++
 tools/libs/light/libxl_pci.c | 41 ++++++++++++++++++++++++++++++++----
 xen/arch/x86/domctl.c        | 31 +++++++++++++++++++++++++++
 xen/arch/x86/hvm/hypercall.c |  9 ++++++++
 xen/arch/x86/physdev.c       | 22 +++++++++++++++++++
 xen/drivers/pci/physdev.c    | 36 +++++++++++++++++++++++++++++++
 xen/drivers/vpci/vpci.c      | 10 +++++++++
 xen/include/public/domctl.h  |  9 ++++++++
 xen/include/public/physdev.h |  7 ++++++
 xen/include/xen/vpci.h       |  6 ++++++
 xen/xsm/flask/hooks.c        |  1 +
 12 files changed, 188 insertions(+), 4 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

RFC PATCH v5 1/5 xen/vpci: Clear all vpci status of device

When a device has been reset on dom0 side, the vpci on Xen side won't get notification, so the cached state in vpci is all out of date compare with the real device state. To solve that problem, add a new hypercall to clear all vpci device state. When the state of device is reset on dom0 side, dom0 can call this hypercall to notify vpci.

---
 xen/arch/x86/hvm/hypercall.c |  1 +
 xen/drivers/pci/physdev.c    | 36 ++++++++++++++++++++++++++++++++++++
 xen/drivers/vpci/vpci.c      | 10 ++++++++++
 xen/include/public/physdev.h |  7 +++++++
 xen/include/xen/vpci.h       |  6 ++++++
 5 files changed, 60 insertions(+)
xen/arch/x86/hvm/hypercall.c +1 -0
index eeb73e1aa5d0..6ad5b4d5f11f 100644
@@ -84,6 +84,7 @@ long hvm_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     case PHYSDEVOP_pci_mmcfg_reserved:
     case PHYSDEVOP_pci_device_add:
     case PHYSDEVOP_pci_device_remove:
+    case PHYSDEVOP_pci_device_state_reset:
     case PHYSDEVOP_dbgp_op:
         if ( !is_hardware_domain(currd) )
             return -ENOSYS;
xen/drivers/pci/physdev.c +36 -0
index 42db3e6d133c..73dc8f058b0e 100644
@@ -2,6 +2,7 @@
 #include <xen/guest_access.h>
 #include <xen/hypercall.h>
 #include <xen/init.h>
+#include <xen/vpci.h>
 
 #ifndef COMPAT
 typedef long ret_t;
@@ -67,6 +68,41 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         break;
     }
 
+    case PHYSDEVOP_pci_device_state_reset: {
+        struct physdev_pci_device dev;
+        struct pci_dev *pdev;
+        pci_sbdf_t sbdf;
+
+        if ( !is_pci_passthrough_enabled() )
+            return -EOPNOTSUPP;
+
+        ret = -EFAULT;
+        if ( copy_from_guest(&dev, arg, 1) != 0 )
+            break;
+        sbdf = PCI_SBDF(dev.seg, dev.bus, dev.devfn);
+
+        ret = xsm_resource_setup_pci(XSM_PRIV, sbdf.sbdf);
+        if ( ret )
+            break;
+
+        pcidevs_lock();
+        pdev = pci_get_pdev(NULL, sbdf);
+        if ( !pdev )
+        {
+            pcidevs_unlock();
+            ret = -ENODEV;
+            break;
+        }
+
+        write_lock(&pdev->domain->pci_lock);
+        ret = vpci_reset_device_state(pdev);
+        write_unlock(&pdev->domain->pci_lock);
+        pcidevs_unlock();
+        if ( ret )
+            printk(XENLOG_ERR "%pp: failed to reset PCI device state\n", &sbdf);
+        break;
+    }
+
     default:
         ret = -ENOSYS;
         break;
xen/drivers/vpci/vpci.c +10 -0
index 72ef277c4f8e..c6df2c6a9561 100644
@@ -107,6 +107,16 @@ int vpci_add_handlers(struct pci_dev *pdev)
 
     return rc;
 }
+
+int vpci_reset_device_state(struct pci_dev *pdev)
+{
+    ASSERT(pcidevs_locked());
+    ASSERT(rw_is_write_locked(&pdev->domain->pci_lock));
+
+    vpci_remove_device(pdev);
+    return vpci_add_handlers(pdev);
+}
+
 #endif /* __XEN__ */
 
 static int vpci_register_cmp(const struct vpci_register *r1,
xen/include/public/physdev.h +7 -0
index f0c0d4727c0b..f5bab1f29779 100644
@@ -296,6 +296,13 @@ DEFINE_XEN_GUEST_HANDLE(physdev_pci_device_add_t);
  */
 #define PHYSDEVOP_prepare_msix          30
 #define PHYSDEVOP_release_msix          31
+/*
+ * Notify the hypervisor that a PCI device has been reset, so that any
+ * internally cached state is regenerated.  Should be called after any
+ * device reset performed by the hardware domain.
+ */
+#define PHYSDEVOP_pci_device_state_reset     32
+
 struct physdev_pci_device {
     /* IN */
     uint16_t seg;
xen/include/xen/vpci.h +6 -0
index d20c301a3db3..6ec83ce9ae13 100644
@@ -30,6 +30,7 @@ int __must_check vpci_add_handlers(struct pci_dev *pdev);
 
 /* Remove all handlers and free vpci related structures. */
 void vpci_remove_device(struct pci_dev *pdev);
+int __must_check vpci_reset_device_state(struct pci_dev *pdev);
 
 /* Add/remove a register handler. */
 int __must_check vpci_add_register_mask(struct vpci *vpci,
@@ -262,6 +263,11 @@ static inline int vpci_add_handlers(struct pci_dev *pdev)
 
 static inline void vpci_remove_device(struct pci_dev *pdev) { }
 
+static inline int __must_check vpci_reset_device_state(struct pci_dev *pdev)
+{
+    return 0;
+}
+
 static inline void vpci_dump_msi(void) { }
 
 static inline uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg,
Signature · 1 line
2.34.1
Reviewed-by on v5 1/5
#2 lore raw

Re: [RFC XEN PATCH v5 1/5] xen/vpci: Clear all vpci status of device

Quoted 9 lines · Jiqian Chen

When a device has been reset on dom0 side, the vpci on Xen side won't get notification, so the cached state in vpci is all out of date compare with the real device state. To solve that problem, add a new hypercall to clear all vpci device state. When the state of device is reset on dom0 side, dom0 can call this hypercall to notify vpci.

If you send another version, the RFC tag may be dropped.

One thing to keep an eye out for below (not requesting any changes).

Quoted 7 lines

---

xen/drivers/vpci/vpci.c +0 -0
index 72ef277c4f8e..c6df2c6a9561 100644
@@ -107,6 +107,16 @@ int vpci_add_handlers(struct pci_dev *pdev)
 

xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 117

Quoted 10 lines
xen/drivers/vpci/vpci.c +8 -0
     return rc;
 }
+
+int vpci_reset_device_state(struct pci_dev *pdev)
+{
+    ASSERT(pcidevs_locked());
+    ASSERT(rw_is_write_locked(&pdev->domain->pci_lock));
+
+    vpci_remove_device(pdev);
+    return vpci_add_handlers(pdev);

Note that these two functions may be renamed soon by the patch at [1]. Whichever patch goes in later will need to be rebased to account for the rename.

[1] https://lists.xenproject.org/archives/html/xen-devel/2024-02/msg00134.html

Quoted 5 lines
xen/drivers/vpci/vpci.c +2 -0
+}
+
 #endif /* __XEN__ */
 
 static int vpci_register_cmp(const struct vpci_register *r1,
#3 lore raw

Re: [RFC XEN PATCH v5 1/5] xen/vpci: Clear all vpci status of device

Hi Stewart,

Quoted 8 lines · Stewart Hildebrand
Quoted 7 lines · Jiqian Chen

When a device has been reset on dom0 side, the vpci on Xen side won't get notification, so the cached state in vpci is all out of date compare with the real device state. To solve that problem, add a new hypercall to clear all vpci device state. When the state of device is reset on dom0 side, dom0 can call this hypercall to notify vpci.

Quoted 4 lines
Quoted 2 lines

Thanks, I will add in next version.

Quoted 2 lines

If you send another version, the RFC tag may be dropped.

Does only this one patch, or all patches of this series, need to drop RFC tag?

Quoted 2 lines

One thing to keep an eye out for below (not requesting any changes).

Thanks for reminding me, I will always keep rebasing my code from latest staging branch before sending new version.

Quoted 31 lines
Quoted 8 lines

---

xen/drivers/vpci/vpci.c +0 -0
index 72ef277c4f8e..c6df2c6a9561 100644
@@ -107,6 +107,16 @@ int vpci_add_handlers(struct pci_dev *pdev)
 

xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 117

Quoted 10 lines
xen/drivers/vpci/vpci.c +8 -0
     return rc;
 }
+
+int vpci_reset_device_state(struct pci_dev *pdev)
+{
+    ASSERT(pcidevs_locked());
+    ASSERT(rw_is_write_locked(&pdev->domain->pci_lock));
+
+    vpci_remove_device(pdev);
+    return vpci_add_handlers(pdev);

Note that these two functions may be renamed soon by the patch at [1]. Whichever patch goes in later will need to be rebased to account for the rename.

[1] https://lists.xenproject.org/archives/html/xen-devel/2024-02/msg00134.html

Quoted 5 lines
xen/drivers/vpci/vpci.c +2 -0
+}
+
 #endif /* __XEN__ */
 
 static int vpci_register_cmp(const struct vpci_register *r1,
Signature · 2 lines
Best regards,
Jiqian Chen.
#4 lore raw

Re: [RFC XEN PATCH v5 1/5] xen/vpci: Clear all vpci status of device

Quoted 14 lines · Jiqian

Hi Stewart,

Quoted 11 lines · Stewart Hildebrand
Quoted 9 lines · Jiqian Chen

When a device has been reset on dom0 side, the vpci on Xen side won't get notification, so the cached state in vpci is all out of date compare with the real device state. To solve that problem, add a new hypercall to clear all vpci device state. When the state of device is reset on dom0 side, dom0 can call this hypercall to notify vpci.

Quoted 6 lines
Quoted 1 line

Thanks, I will add in next version.

Quoted 2 lines

If you send another version, the RFC tag may be dropped.

Does only this one patch, or all patches of this series, need to drop RFC tag?

In my opinion at least patches 1, 2, and 3. If you decide to retain the RFC tag on e.g. patches 4 and/or 5, you may want to include after the commit description a scissors line --- followed by a brief explanation for the RFC tag. For example:

---
RFC: discussions ongoing on the Linux side where/how to expose the gsi

Quoted 36 lines
Quoted 2 lines

One thing to keep an eye out for below (not requesting any changes).

Thanks for reminding me, I will always keep rebasing my code from latest staging branch before sending new version.

Quoted 31 lines
Quoted 8 lines

---

xen/drivers/vpci/vpci.c +0 -0
index 72ef277c4f8e..c6df2c6a9561 100644
@@ -107,6 +107,16 @@ int vpci_add_handlers(struct pci_dev *pdev)
 

xen/drivers/vpci/vpci.c int vpci_add_handlers(struct pci_dev *pdev) · line 117

Quoted 10 lines
xen/drivers/vpci/vpci.c +8 -0
     return rc;
 }
+
+int vpci_reset_device_state(struct pci_dev *pdev)
+{
+    ASSERT(pcidevs_locked());
+    ASSERT(rw_is_write_locked(&pdev->domain->pci_lock));
+
+    vpci_remove_device(pdev);
+    return vpci_add_handlers(pdev);

Note that these two functions may be renamed soon by the patch at [1]. Whichever patch goes in later will need to be rebased to account for the rename.

[1] https://lists.xenproject.org/archives/html/xen-devel/2024-02/msg00134.html

Quoted 5 lines
xen/drivers/vpci/vpci.c +2 -0
+}
+
 #endif /* __XEN__ */
 
 static int vpci_register_cmp(const struct vpci_register *r1,
Reviewed-by
#5 lore raw

Re: [RFC XEN PATCH v5 1/5] xen/vpci: Clear all vpci status of device

In reply to Stewart Hildebrand
Quoted 8 lines · Stewart Hildebrand
Quoted 7 lines · Jiqian Chen

When a device has been reset on dom0 side, the vpci on Xen side won't get notification, so the cached state in vpci is all out of date compare with the real device state. To solve that problem, add a new hypercall to clear all vpci device state. When the state of device is reset on dom0 side, dom0 can call this hypercall to notify vpci.

Quoted 4 lines
Quoted 2 lines
#6 lore raw

RFC PATCH v5 2/5 x86/pvh: Allow (un)map_pirq when dom0 is PVH

In reply to Jiqian Chen

If run Xen with PVH dom0 and hvm domU, hvm will map a pirq for a passthrough device by using gsi, see xen_pt_realize->xc_physdev_map_pirq and pci_add_dm_done->xc_physdev_map_pirq. Then xc_physdev_map_pirq will call into Xen, but in hvm_physdev_op, PHYSDEVOP_map_pirq is not allowed because currd is PVH dom0 and PVH has no X86_EMU_USE_PIRQ flag, it will fail at has_pirq check.

So, allow PHYSDEVOP_map_pirq when dom0 is PVH and also allow PHYSDEVOP_unmap_pirq for the failed path to unmap pirq. And add a new check to prevent self map when caller has no PIRQ flag.

---
 xen/arch/x86/hvm/hypercall.c |  2 ++
 xen/arch/x86/physdev.c       | 22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+)
xen/arch/x86/hvm/hypercall.c +2 -0
index 6ad5b4d5f11f..493998b42ec5 100644
@@ -74,6 +74,8 @@ long hvm_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     {
     case PHYSDEVOP_map_pirq:
     case PHYSDEVOP_unmap_pirq:
+        break;
+
     case PHYSDEVOP_eoi:
     case PHYSDEVOP_irq_status_query:
     case PHYSDEVOP_get_free_pirq:
xen/arch/x86/physdev.c +22 -0
index 47c4da0af7e1..7f2422c2a483 100644
@@ -303,11 +303,22 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     case PHYSDEVOP_map_pirq: {
         physdev_map_pirq_t map;
         struct msi_info msi;
+        struct domain *d;
 
         ret = -EFAULT;
         if ( copy_from_guest(&map, arg, 1) != 0 )
             break;
 
+        d = rcu_lock_domain_by_any_id(map.domid);
+        if ( d == NULL )
+            return -ESRCH;
+        if ( !is_pv_domain(d) && !has_pirq(d) )
+        {
+            rcu_unlock_domain(d);
+            return -EOPNOTSUPP;
+        }
+        rcu_unlock_domain(d);
+
         switch ( map.type )
         {
         case MAP_PIRQ_TYPE_MSI_SEG:
@@ -341,11 +352,22 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
     case PHYSDEVOP_unmap_pirq: {
         struct physdev_unmap_pirq unmap;
+        struct domain *d;
 
         ret = -EFAULT;
         if ( copy_from_guest(&unmap, arg, 1) != 0 )
             break;
 
+        d = rcu_lock_domain_by_any_id(unmap.domid);
+        if ( d == NULL )
+            return -ESRCH;
+        if ( !is_pv_domain(d) && !has_pirq(d) )
+        {
+            rcu_unlock_domain(d);
+            return -EOPNOTSUPP;
+        }
+        rcu_unlock_domain(d);
+
         ret = physdev_unmap_pirq(unmap.domid, unmap.pirq);
         break;
     }
Signature · 1 line
2.34.1
#7 lore raw

Re: [RFC XEN PATCH v5 2/5] x86/pvh: Allow (un)map_pirq when dom0 is PVH

Quoted 41 lines · Jiqian Chen

If run Xen with PVH dom0 and hvm domU, hvm will map a pirq for a passthrough device by using gsi, see xen_pt_realize->xc_physdev_map_pirq and pci_add_dm_done->xc_physdev_map_pirq. Then xc_physdev_map_pirq will call into Xen, but in hvm_physdev_op, PHYSDEVOP_map_pirq is not allowed because currd is PVH dom0 and PVH has no X86_EMU_USE_PIRQ flag, it will fail at has_pirq check.

So, allow PHYSDEVOP_map_pirq when dom0 is PVH and also allow PHYSDEVOP_unmap_pirq for the failed path to unmap pirq. And add a new check to prevent self map when caller has no PIRQ flag.

---
 xen/arch/x86/hvm/hypercall.c |  2 ++
 xen/arch/x86/physdev.c       | 22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+)
xen/arch/x86/hvm/hypercall.c +2 -0
index 6ad5b4d5f11f..493998b42ec5 100644
@@ -74,6 +74,8 @@ long hvm_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     {
     case PHYSDEVOP_map_pirq:
     case PHYSDEVOP_unmap_pirq:
+        break;
+
     case PHYSDEVOP_eoi:
     case PHYSDEVOP_irq_status_query:
     case PHYSDEVOP_get_free_pirq:
xen/arch/x86/physdev.c +0 -0
index 47c4da0af7e1..7f2422c2a483 100644
@@ -303,11 +303,22 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     case PHYSDEVOP_map_pirq: {
         physdev_map_pirq_t map;
         struct msi_info msi;

xen/arch/x86/physdev.c ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 315

Quoted 10 lines
xen/arch/x86/physdev.c +5 -0
+        struct domain *d;
 
         ret = -EFAULT;
         if ( copy_from_guest(&map, arg, 1) != 0 )
             break;
 
+        d = rcu_lock_domain_by_any_id(map.domid);
+        if ( d == NULL )
+            return -ESRCH;
+        if ( !is_pv_domain(d) && !has_pirq(d) )

I think this could just be:

    if ( !has_pirq(d) )

Right?

Quoted 13 lines
xen/arch/x86/physdev.c +6 -0
+        {
+            rcu_unlock_domain(d);
+            return -EOPNOTSUPP;
+        }
+        rcu_unlock_domain(d);
+
         switch ( map.type )
         {
         case MAP_PIRQ_TYPE_MSI_SEG:
@@ -341,11 +352,22 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
     case PHYSDEVOP_unmap_pirq: {
         struct physdev_unmap_pirq unmap;

xen/arch/x86/physdev.c ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 364

Quoted 10 lines
xen/arch/x86/physdev.c +5 -0
+        struct domain *d;
 
         ret = -EFAULT;
         if ( copy_from_guest(&unmap, arg, 1) != 0 )
             break;
 
+        d = rcu_lock_domain_by_any_id(unmap.domid);
+        if ( d == NULL )
+            return -ESRCH;
+        if ( !is_pv_domain(d) && !has_pirq(d) )

same here

Other than that, everything looks fine to me

Quoted 12 lines
xen/arch/x86/physdev.c +6 -0
+        {
+            rcu_unlock_domain(d);
+            return -EOPNOTSUPP;
+        }
+        rcu_unlock_domain(d);
+
         ret = physdev_unmap_pirq(unmap.domid, unmap.pirq);
         break;
     }
Signature · 1 line
2.34.1
#8 lore raw

Re: [RFC XEN PATCH v5 2/5] x86/pvh: Allow (un)map_pirq when dom0 is PVH

Quoted 55 lines · Stefano Stabellini
Quoted 41 lines · Jiqian Chen

If run Xen with PVH dom0 and hvm domU, hvm will map a pirq for a passthrough device by using gsi, see xen_pt_realize->xc_physdev_map_pirq and pci_add_dm_done->xc_physdev_map_pirq. Then xc_physdev_map_pirq will call into Xen, but in hvm_physdev_op, PHYSDEVOP_map_pirq is not allowed because currd is PVH dom0 and PVH has no X86_EMU_USE_PIRQ flag, it will fail at has_pirq check.

So, allow PHYSDEVOP_map_pirq when dom0 is PVH and also allow PHYSDEVOP_unmap_pirq for the failed path to unmap pirq. And add a new check to prevent self map when caller has no PIRQ flag.

---
 xen/arch/x86/hvm/hypercall.c |  2 ++
 xen/arch/x86/physdev.c       | 22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+)
xen/arch/x86/hvm/hypercall.c +2 -0
index 6ad5b4d5f11f..493998b42ec5 100644
@@ -74,6 +74,8 @@ long hvm_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     {
     case PHYSDEVOP_map_pirq:
     case PHYSDEVOP_unmap_pirq:
+        break;
+
     case PHYSDEVOP_eoi:
     case PHYSDEVOP_irq_status_query:
     case PHYSDEVOP_get_free_pirq:
xen/arch/x86/physdev.c +0 -0
index 47c4da0af7e1..7f2422c2a483 100644
@@ -303,11 +303,22 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     case PHYSDEVOP_map_pirq: {
         physdev_map_pirq_t map;
         struct msi_info msi;

xen/arch/x86/physdev.c ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 315

Quoted 10 lines
xen/arch/x86/physdev.c +5 -0
+        struct domain *d;
 
         ret = -EFAULT;
         if ( copy_from_guest(&map, arg, 1) != 0 )
             break;
 
+        d = rcu_lock_domain_by_any_id(map.domid);
+        if ( d == NULL )
+            return -ESRCH;
+        if ( !is_pv_domain(d) && !has_pirq(d) )

I think this could just be:

Quoted 3 lines
    if ( !has_pirq(d) )

Right?

No. In the beginning, I only set this condition here, but it destroyed PV dom0.
Because PV has no pirq flag too, it can match this condition and return -EOPNOTSUPP, PHYSDEVOP_map_pirq will fail.

Quoted 44 lines
Quoted 15 lines
xen/arch/x86/physdev.c +6 -0
+        {
+            rcu_unlock_domain(d);
+            return -EOPNOTSUPP;
+        }
+        rcu_unlock_domain(d);
+
         switch ( map.type )
         {
         case MAP_PIRQ_TYPE_MSI_SEG:
@@ -341,11 +352,22 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
     case PHYSDEVOP_unmap_pirq: {
         struct physdev_unmap_pirq unmap;

xen/arch/x86/physdev.c ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 364

Quoted 10 lines
xen/arch/x86/physdev.c +5 -0
+        struct domain *d;
 
         ret = -EFAULT;
         if ( copy_from_guest(&unmap, arg, 1) != 0 )
             break;
 
+        d = rcu_lock_domain_by_any_id(unmap.domid);
+        if ( d == NULL )
+            return -ESRCH;
+        if ( !is_pv_domain(d) && !has_pirq(d) )

same here

Other than that, everything looks fine to me

Quoted 12 lines
xen/arch/x86/physdev.c +6 -0
+        {
+            rcu_unlock_domain(d);
+            return -EOPNOTSUPP;
+        }
+        rcu_unlock_domain(d);
+
         ret = physdev_unmap_pirq(unmap.domid, unmap.pirq);
         break;
     }
Signature · 1 line
2.34.1
Signature · 2 lines
Best regards,
Jiqian Chen.
Reviewed-by
#9 lore raw

Re: [RFC XEN PATCH v5 2/5] x86/pvh: Allow (un)map_pirq when dom0 is PVH

Quoted 56 lines · Jiqian
Quoted 55 lines · Stefano Stabellini
Quoted 41 lines · Jiqian Chen

If run Xen with PVH dom0 and hvm domU, hvm will map a pirq for a passthrough device by using gsi, see xen_pt_realize->xc_physdev_map_pirq and pci_add_dm_done->xc_physdev_map_pirq. Then xc_physdev_map_pirq will call into Xen, but in hvm_physdev_op, PHYSDEVOP_map_pirq is not allowed because currd is PVH dom0 and PVH has no X86_EMU_USE_PIRQ flag, it will fail at has_pirq check.

So, allow PHYSDEVOP_map_pirq when dom0 is PVH and also allow PHYSDEVOP_unmap_pirq for the failed path to unmap pirq. And add a new check to prevent self map when caller has no PIRQ flag.

---
 xen/arch/x86/hvm/hypercall.c |  2 ++
 xen/arch/x86/physdev.c       | 22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+)
xen/arch/x86/hvm/hypercall.c +2 -0
index 6ad5b4d5f11f..493998b42ec5 100644
@@ -74,6 +74,8 @@ long hvm_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     {
     case PHYSDEVOP_map_pirq:
     case PHYSDEVOP_unmap_pirq:
+        break;
+
     case PHYSDEVOP_eoi:
     case PHYSDEVOP_irq_status_query:
     case PHYSDEVOP_get_free_pirq:
xen/arch/x86/physdev.c +0 -0
index 47c4da0af7e1..7f2422c2a483 100644
@@ -303,11 +303,22 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     case PHYSDEVOP_map_pirq: {
         physdev_map_pirq_t map;
         struct msi_info msi;

xen/arch/x86/physdev.c ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 315

Quoted 10 lines
xen/arch/x86/physdev.c +5 -0
+        struct domain *d;
 
         ret = -EFAULT;
         if ( copy_from_guest(&map, arg, 1) != 0 )
             break;
 
+        d = rcu_lock_domain_by_any_id(map.domid);
+        if ( d == NULL )
+            return -ESRCH;
+        if ( !is_pv_domain(d) && !has_pirq(d) )

I think this could just be:

Quoted 5 lines
Quoted 3 lines
    if ( !has_pirq(d) )

Right?

No. In the beginning, I only set this condition here, but it destroyed PV dom0.
Because PV has no pirq flag too, it can match this condition and return -EOPNOTSUPP, PHYSDEVOP_map_pirq will fail.

Yes I get it now. Thanks for the explanation. I think "has_pirq" is misnamed and should probably be hvm_has_pirq or something like that.

I am not asking to fix it, but maybe an in-code comment here would help, like:

/* if it is an HVM guest, check if it has PIRQs */

in any case:

#10 lore raw

RFC PATCH v5 3/5 x86/pvh: Add PHYSDEVOP_setup_gsi for PVH dom0

In reply to Jiqian Chen

On PVH dom0, the gsis don't get registered, but the gsi of a passthrough device must be configured for it to be able to be mapped into a hvm domU.
On Linux kernel side, it calles PHYSDEVOP_setup_gsi for passthrough devices to register gsi when dom0 is PVH. So, add PHYSDEVOP_setup_gsi for above purpose.

---
 xen/arch/x86/hvm/hypercall.c | 6 ++++++
 1 file changed, 6 insertions(+)
xen/arch/x86/hvm/hypercall.c +6 -0
index 493998b42ec5..46f51ee459f6 100644
@@ -76,6 +76,12 @@ long hvm_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     case PHYSDEVOP_unmap_pirq:
         break;
 
+    case PHYSDEVOP_setup_gsi:
+        if ( !is_hardware_domain(currd) )
+            return -EOPNOTSUPP;
+        ASSERT(!has_pirq(currd));
+        break;
+
     case PHYSDEVOP_eoi:
     case PHYSDEVOP_irq_status_query:
     case PHYSDEVOP_get_free_pirq:
Signature · 1 line
2.34.1
#11 lore raw

Re: [RFC XEN PATCH v5 3/5] x86/pvh: Add PHYSDEVOP_setup_gsi for PVH dom0

Quoted 18 lines · Jiqian Chen

On PVH dom0, the gsis don't get registered, but the gsi of a passthrough device must be configured for it to be able to be mapped into a hvm domU.
On Linux kernel side, it calles PHYSDEVOP_setup_gsi for passthrough devices to register gsi when dom0 is PVH. So, add PHYSDEVOP_setup_gsi for above purpose.

---
 xen/arch/x86/hvm/hypercall.c | 6 ++++++
 1 file changed, 6 insertions(+)
xen/arch/x86/hvm/hypercall.c +0 -0
index 493998b42ec5..46f51ee459f6 100644

xen/arch/x86/hvm/hypercall.c long hvm_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 82

Quoted 7 lines
xen/arch/x86/hvm/hypercall.c +4 -0
@@ -76,6 +76,12 @@ long hvm_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     case PHYSDEVOP_unmap_pirq:
         break;
 
+    case PHYSDEVOP_setup_gsi:
+        if ( !is_hardware_domain(currd) )
+            return -EOPNOTSUPP;
+        ASSERT(!has_pirq(currd));

Do we really need this assert? I understand that the use case right now is for !has_pirq(currd) but in general it doesn't seem to me that PHYSDEVOP_setup_gsi and !has_pirq should be tied together.

Aside from that, it looks fine.

Quoted 8 lines
xen/arch/x86/hvm/hypercall.c +2 -0
+        break;
+
     case PHYSDEVOP_eoi:
     case PHYSDEVOP_irq_status_query:
     case PHYSDEVOP_get_free_pirq:
Signature · 1 line
2.34.1
#12 lore raw

Re: [RFC XEN PATCH v5 3/5] x86/pvh: Add PHYSDEVOP_setup_gsi for PVH dom0

Quoted 22 lines · Stefano Stabellini
Quoted 21 lines · Jiqian Chen

On PVH dom0, the gsis don't get registered, but the gsi of a passthrough device must be configured for it to be able to be mapped into a hvm domU.
On Linux kernel side, it calles PHYSDEVOP_setup_gsi for passthrough devices to register gsi when dom0 is PVH. So, add PHYSDEVOP_setup_gsi for above purpose.

---
 xen/arch/x86/hvm/hypercall.c | 6 ++++++
 1 file changed, 6 insertions(+)
xen/arch/x86/hvm/hypercall.c +0 -0
index 493998b42ec5..46f51ee459f6 100644
@@ -76,6 +76,12 @@ long hvm_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     case PHYSDEVOP_unmap_pirq:
         break;
 
Quoted 8 lines

xen/arch/x86/hvm/hypercall.c long hvm_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 82

Quoted 4 lines
xen/arch/x86/hvm/hypercall.c +4 -0
+    case PHYSDEVOP_setup_gsi:
+        if ( !is_hardware_domain(currd) )
+            return -EOPNOTSUPP;
+        ASSERT(!has_pirq(currd));

Do we really need this assert? I understand that the use case right now is for !has_pirq(currd) but in general it doesn't seem to me that PHYSDEVOP_setup_gsi and !has_pirq should be tied together.

Make sense, thanks for explanation, I will delete this in next version.

Quoted 12 lines

Aside from that, it looks fine.

Quoted 8 lines
xen/arch/x86/hvm/hypercall.c +2 -0
+        break;
+
     case PHYSDEVOP_eoi:
     case PHYSDEVOP_irq_status_query:
     case PHYSDEVOP_get_free_pirq:
Signature · 1 line
2.34.1
Signature · 2 lines
Best regards,
Jiqian Chen.
#13 lore raw

RFC PATCH v5 4/5 libxl: Use gsi instead of irq for mapping pirq

In reply to Jiqian Chen

In PVH dom0, it uses the linux local interrupt mechanism, when it allocs irq for a gsi, it is dynamic, and follow the principle of applying first, distributing first. And the irq number is alloced from small to large, but the applying gsi number is not, may gsi 38 comes before gsi 28, that causes the irq number is not equal with the gsi number. And when passthrough a device, xl wants to use gsi to map pirq, see pci_add_dm_done->xc_physdev_map_pirq, but the gsi number is got from file /sys/bus/pci/devices/<sbdf>/irq in current code, so it will fail when mapping.

So, use real gsi number read from gsi sysfs.

---
 tools/libs/light/libxl_pci.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)
tools/libs/light/libxl_pci.c +23 -2
index 96cb4da0794e..a1c6e82631e9 100644
@@ -1478,8 +1478,19 @@ static void pci_add_dm_done(libxl__egc *egc,
     fclose(f);
     if (!pci_supp_legacy_irq())
         goto out_no_irq;
-    sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/irq", pci->domain,
+    sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/gsi", pci->domain,
                                 pci->bus, pci->dev, pci->func);
+
+    if ( access(sysfs_path, F_OK) != 0 ) {
+        if ( errno == ENOENT )
+            sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/irq", pci->domain,
+                                pci->bus, pci->dev, pci->func);
+        else {
+            LOGED(ERROR, domainid, "Can't access %s", sysfs_path);
+            goto out_no_irq;
+        }
+    }
+
     f = fopen(sysfs_path, "r");
     if (f == NULL) {
         LOGED(ERROR, domainid, "Couldn't open %s", sysfs_path);
@@ -2229,9 +2240,19 @@ skip_bar:
     if (!pci_supp_legacy_irq())
         goto skip_legacy_irq;
 
-    sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/irq", pci->domain,
+    sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/gsi", pci->domain,
                            pci->bus, pci->dev, pci->func);
 
+    if ( access(sysfs_path, F_OK) != 0 ) {
+        if ( errno == ENOENT )
+            sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/irq", pci->domain,
+                                pci->bus, pci->dev, pci->func);
+        else {
+            LOGED(ERROR, domid, "Can't access %s", sysfs_path);
+            goto skip_legacy_irq;
+        }
+    }
+
     f = fopen(sysfs_path, "r");
     if (f == NULL) {
         LOGED(ERROR, domid, "Couldn't open %s", sysfs_path);
Signature · 1 line
2.34.1
#14 lore raw

Re: [RFC XEN PATCH v5 4/5] libxl: Use gsi instead of irq for mapping pirq

Hi Jiqian,

Quoted 12 lines · Jiqian Chen

In PVH dom0, it uses the linux local interrupt mechanism, when it allocs irq for a gsi, it is dynamic, and follow the principle of applying first, distributing first. And the irq number is alloced from small to large, but the applying gsi number is not, may gsi 38 comes before gsi 28, that causes the irq number is not equal with the gsi number. And when passthrough a device, xl wants to use gsi to map pirq, see pci_add_dm_done->xc_physdev_map_pirq, but the gsi number is got from file /sys/bus/pci/devices/<sbdf>/irq in current code, so it will fail when mapping.

Quoted 3 lines

So, use real gsi number read from gsi sysfs.

The "Co-developed-by" tag isn't really defined in Xen, it's fine to keep I guess, but it mean that another tag is missing, I'm pretty sure you need to add "Signed-off-by: Huang Rui <[email protected]>"

Beyond that, the patch looks good, I've only coding style issues.

Quoted 11 lines
---
 tools/libs/light/libxl_pci.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)
tools/libs/light/libxl_pci.c +0 -0
index 96cb4da0794e..a1c6e82631e9 100644

tools/libs/light/libxl_pci.c static void pci_add_dm_done(libxl__egc *egc, · line 1484

Quoted 8 lines
tools/libs/light/libxl_pci.c +3 -1
@@ -1478,8 +1478,19 @@ static void pci_add_dm_done(libxl__egc *egc,
     fclose(f);
     if (!pci_supp_legacy_irq())
         goto out_no_irq;
-    sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/irq", pci->domain,
+    sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/gsi", pci->domain,
                                 pci->bus, pci->dev, pci->func);
+
+    if ( access(sysfs_path, F_OK) != 0 ) {

So, the coding style in libxl is a bit different, first could you store the return value of access() in 'r', then check that value? Then "if (r)" will be enough, no need for "!= 0".

Second, there's no around space the condition in the if statement, so just "if (r)".

tools/libs/light/libxl_pci.c static void pci_add_dm_done(libxl__egc *egc, · line 1487

Quoted 3 lines
tools/libs/light/libxl_pci.c +3 -0
+        if ( errno == ENOENT )
+            sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/irq", pci->domain,
+                                pci->bus, pci->dev, pci->func);

Has the else part of this if..else is in a {}-block, could you add { } here, for the if part? To quote the CODING_STYLE libxl document: "To avoid confusion, either all the blocks in an if...else chain have braces, or none of them do.

tools/libs/light/libxl_pci.c static void pci_add_dm_done(libxl__egc *egc, · line 1489

Quoted 2 lines
tools/libs/light/libxl_pci.c +2 -0
+        else {
+            LOGED(ERROR, domainid, "Can't access %s", sysfs_path);

This error message is kind of redundant, we could could simply let the code try fopen() on this "/gsi" path, even if we know that it's not going to work, as the error check on fopen() will produce the same error message. ;-)

And without that else part, the code could be simplified to:

    /* If /gsi path doesn't exist, fallback to /irq */
    if (r && errno == ENOENT)
        sysfs_path = "..../irq";

And those comments also apply to the changes in pci_remove_detached().

Thanks,

Signature · 1 line
Anthony PERARD
#15 lore raw

Re: [RFC XEN PATCH v5 4/5] libxl: Use gsi instead of irq for mapping pirq

Hi Anthony,

Quoted 15 lines · Anthony PERARD

Hi Jiqian,

Quoted 12 lines · Jiqian Chen

In PVH dom0, it uses the linux local interrupt mechanism, when it allocs irq for a gsi, it is dynamic, and follow the principle of applying first, distributing first. And the irq number is alloced from small to large, but the applying gsi number is not, may gsi 38 comes before gsi 28, that causes the irq number is not equal with the gsi number. And when passthrough a device, xl wants to use gsi to map pirq, see pci_add_dm_done->xc_physdev_map_pirq, but the gsi number is got from file /sys/bus/pci/devices/<sbdf>/irq in current code, so it will fail when mapping.

Quoted 7 lines
Quoted 3 lines

So, use real gsi number read from gsi sysfs.

The "Co-developed-by" tag isn't really defined in Xen, it's fine to keep I guess, but it mean that another tag is missing, I'm pretty sure you need to add "Signed-off-by: Huang Rui <[email protected]>"

Ok, will add this in next version.

Quoted 2 lines

Beyond that, the patch looks good, I've only coding style issues.

There are two encoding styles in the current file, and I was also struggling with which one to follow when I write my code, it seems that I made the wrong choice. Thank you very much for your suggestions. I will fix all coding style issues that you pointed out in next version.

Quoted 57 lines
Quoted 12 lines
---
 tools/libs/light/libxl_pci.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)
tools/libs/light/libxl_pci.c +0 -0
index 96cb4da0794e..a1c6e82631e9 100644

tools/libs/light/libxl_pci.c static void pci_add_dm_done(libxl__egc *egc, · line 1484

Quoted 8 lines
tools/libs/light/libxl_pci.c +3 -1
@@ -1478,8 +1478,19 @@ static void pci_add_dm_done(libxl__egc *egc,
     fclose(f);
     if (!pci_supp_legacy_irq())
         goto out_no_irq;
-    sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/irq", pci->domain,
+    sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/gsi", pci->domain,
                                 pci->bus, pci->dev, pci->func);
+
+    if ( access(sysfs_path, F_OK) != 0 ) {

So, the coding style in libxl is a bit different, first could you store the return value of access() in 'r', then check that value? Then "if (r)" will be enough, no need for "!= 0".

Second, there's no around space the condition in the if statement, so just "if (r)".

tools/libs/light/libxl_pci.c static void pci_add_dm_done(libxl__egc *egc, · line 1487

Quoted 3 lines
tools/libs/light/libxl_pci.c +3 -0
+        if ( errno == ENOENT )
+            sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/irq", pci->domain,
+                                pci->bus, pci->dev, pci->func);

Has the else part of this if..else is in a {}-block, could you add { } here, for the if part? To quote the CODING_STYLE libxl document: "To avoid confusion, either all the blocks in an if...else chain have braces, or none of them do.

tools/libs/light/libxl_pci.c static void pci_add_dm_done(libxl__egc *egc, · line 1489

Quoted 2 lines
tools/libs/light/libxl_pci.c +2 -0
+        else {
+            LOGED(ERROR, domainid, "Can't access %s", sysfs_path);

This error message is kind of redundant, we could could simply let the code try fopen() on this "/gsi" path, even if we know that it's not going to work, as the error check on fopen() will produce the same error message. ;-)

And without that else part, the code could be simplified to:

    /* If /gsi path doesn't exist, fallback to /irq */
    if (r && errno == ENOENT)
        sysfs_path = "..../irq";

And those comments also apply to the changes in pci_remove_detached().

Thanks,

Signature · 2 lines
Best regards,
Jiqian Chen.
#16 lore raw

RFC PATCH v5 5/5 domctl: Add XEN_DOMCTL_gsi_permission to grant gsi

In reply to Jiqian Chen

Some type of domain don't have PIRQ, like PVH, when passthrough a device to guest on PVH dom0, callstack pci_add_dm_done->XEN_DOMCTL_irq_permission will failed at domain_pirq_to_irq.

So, add a new hypercall to grant/revoke gsi permission when dom0 is not PV or dom0 has not PIRQ flag.

---
 tools/include/xenctrl.h      |  5 +++++
 tools/libs/ctrl/xc_domain.c  | 15 +++++++++++++++
 tools/libs/light/libxl_pci.c | 16 ++++++++++++++--
 xen/arch/x86/domctl.c        | 31 +++++++++++++++++++++++++++++++
 xen/include/public/domctl.h  |  9 +++++++++
 xen/xsm/flask/hooks.c        |  1 +
 6 files changed, 75 insertions(+), 2 deletions(-)
tools/include/xenctrl.h +5 -0
index 2ef8b4e05422..519c860a00d5 100644
@@ -1382,6 +1382,11 @@ int xc_domain_irq_permission(xc_interface *xch,
                              uint32_t pirq,
                              bool allow_access);
 
+int xc_domain_gsi_permission(xc_interface *xch,
+                             uint32_t domid,
+                             uint32_t gsi,
+                             bool allow_access);
+
 int xc_domain_iomem_permission(xc_interface *xch,
                                uint32_t domid,
                                unsigned long first_mfn,
tools/libs/ctrl/xc_domain.c +15 -0
index f2d9d14b4d9f..448ba2c59ae1 100644
@@ -1394,6 +1394,21 @@ int xc_domain_irq_permission(xc_interface *xch,
     return do_domctl(xch, &domctl);
 }
 
+int xc_domain_gsi_permission(xc_interface *xch,
+                             uint32_t domid,
+                             uint32_t gsi,
+                             bool allow_access)
+{
+    struct xen_domctl domctl = {};
+
+    domctl.cmd = XEN_DOMCTL_gsi_permission;
+    domctl.domain = domid;
+    domctl.u.gsi_permission.gsi = gsi;
+    domctl.u.gsi_permission.allow_access = allow_access;
+
+    return do_domctl(xch, &domctl);
+}
+
 int xc_domain_iomem_permission(xc_interface *xch,
                                uint32_t domid,
                                unsigned long first_mfn,
tools/libs/light/libxl_pci.c +14 -2
index a1c6e82631e9..4136a860a048 100644
@@ -1421,6 +1421,8 @@ static void pci_add_dm_done(libxl__egc *egc,
     uint32_t flag = XEN_DOMCTL_DEV_RDM_RELAXED;
     uint32_t domainid = domid;
     bool isstubdom = libxl_is_stubdom(ctx, domid, &domainid);
+    int gsi;
+    bool has_gsi = true;
 
     /* Convenience aliases */
     bool starting = pas->starting;
@@ -1482,6 +1484,7 @@ static void pci_add_dm_done(libxl__egc *egc,
                                 pci->bus, pci->dev, pci->func);
 
     if ( access(sysfs_path, F_OK) != 0 ) {
+        has_gsi = false;
         if ( errno == ENOENT )
             sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/irq", pci->domain,
                                 pci->bus, pci->dev, pci->func);
@@ -1497,6 +1500,7 @@ static void pci_add_dm_done(libxl__egc *egc,
         goto out_no_irq;
     }
     if ((fscanf(f, "%u", &irq) == 1) && irq) {
+        gsi = irq;
         r = xc_physdev_map_pirq(ctx->xch, domid, irq, &irq);
         if (r < 0) {
             LOGED(ERROR, domainid, "xc_physdev_map_pirq irq=%d (error=%d)",
@@ -1505,7 +1509,10 @@ static void pci_add_dm_done(libxl__egc *egc,
             rc = ERROR_FAIL;
             goto out;
         }
-        r = xc_domain_irq_permission(ctx->xch, domid, irq, 1);
+        if ( has_gsi )
+            r = xc_domain_gsi_permission(ctx->xch, domid, gsi, 1);
+        if ( !has_gsi || r == -EOPNOTSUPP )
+            r = xc_domain_irq_permission(ctx->xch, domid, irq, 1);
         if (r < 0) {
             LOGED(ERROR, domainid,
                   "xc_domain_irq_permission irq=%d (error=%d)", irq, r);
@@ -2185,6 +2192,7 @@ static void pci_remove_detached(libxl__egc *egc,
     FILE *f;
     uint32_t domainid = prs->domid;
     bool isstubdom;
+    bool has_gsi = true;
 
     /* Convenience aliases */
     libxl_device_pci *const pci = &prs->pci;
@@ -2244,6 +2252,7 @@ skip_bar:
                            pci->bus, pci->dev, pci->func);
 
     if ( access(sysfs_path, F_OK) != 0 ) {
+        has_gsi = false;
         if ( errno == ENOENT )
             sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/irq", pci->domain,
                                 pci->bus, pci->dev, pci->func);
@@ -2270,7 +2279,10 @@ skip_bar:
              */
             LOGED(ERROR, domid, "xc_physdev_unmap_pirq irq=%d", irq);
         }
-        rc = xc_domain_irq_permission(ctx->xch, domid, irq, 0);
+        if ( has_gsi )
+            rc = xc_domain_gsi_permission(ctx->xch, domid, irq, 0);
+        if ( !has_gsi || rc == -EOPNOTSUPP )
+            rc = xc_domain_irq_permission(ctx->xch, domid, irq, 0);
         if (rc < 0) {
             LOGED(ERROR, domid, "xc_domain_irq_permission irq=%d", irq);
         }
xen/arch/x86/domctl.c +31 -0
index 3c0ea8554107..75f23f511ac1 100644
@@ -237,6 +237,37 @@ long arch_do_domctl(
         break;
     }
 
+    case XEN_DOMCTL_gsi_permission:
+    {
+        unsigned int gsi = domctl->u.gsi_permission.gsi;
+        int allow = domctl->u.gsi_permission.allow_access;
+
+        if ( is_pv_domain(current->domain) || has_pirq(current->domain) )
+        {
+            ret = -EOPNOTSUPP;
+            break;
+        }
+
+        if ( gsi >= nr_irqs_gsi )
+        {
+            ret = -EINVAL;
+            break;
+        }
+
+        if ( !irq_access_permitted(current->domain, gsi) ||
+             xsm_irq_permission(XSM_HOOK, d, gsi, allow) )
+        {
+            ret = -EPERM;
+            break;
+        }
+
+        if ( allow )
+            ret = irq_permit_access(d, gsi);
+        else
+            ret = irq_deny_access(d, gsi);
+        break;
+    }
+
     case XEN_DOMCTL_getpageframeinfo3:
     {
         unsigned int num = domctl->u.getpageframeinfo3.num;
xen/include/public/domctl.h +9 -0
index a33f9ec32b08..47e95f9ee824 100644
@@ -447,6 +447,13 @@ struct xen_domctl_irq_permission {
 };
 
 
+/* XEN_DOMCTL_gsi_permission */
+struct xen_domctl_gsi_permission {
+    uint32_t gsi;
+    uint8_t allow_access;    /* flag to specify enable/disable of x86 gsi access */
+};
+
+
 /* XEN_DOMCTL_iomem_permission */
 struct xen_domctl_iomem_permission {
     uint64_aligned_t first_mfn;/* first page (physical page number) in range */
@@ -1277,6 +1284,7 @@ struct xen_domctl {
 #define XEN_DOMCTL_vmtrace_op                    84
 #define XEN_DOMCTL_get_paging_mempool_size       85
 #define XEN_DOMCTL_set_paging_mempool_size       86
+#define XEN_DOMCTL_gsi_permission                87
 #define XEN_DOMCTL_gdbsx_guestmemio            1000
 #define XEN_DOMCTL_gdbsx_pausevcpu             1001
 #define XEN_DOMCTL_gdbsx_unpausevcpu           1002
@@ -1299,6 +1307,7 @@ struct xen_domctl {
         struct xen_domctl_setdomainhandle   setdomainhandle;
         struct xen_domctl_setdebugging      setdebugging;
         struct xen_domctl_irq_permission    irq_permission;
+        struct xen_domctl_gsi_permission    gsi_permission;
         struct xen_domctl_iomem_permission  iomem_permission;
         struct xen_domctl_ioport_permission ioport_permission;
         struct xen_domctl_hypercall_init    hypercall_init;
xen/xsm/flask/hooks.c +1 -0
index 78225f68c15c..376076865198 100644
@@ -685,6 +685,7 @@ static int cf_check flask_domctl(struct domain *d, int cmd)
     case XEN_DOMCTL_shadow_op:
     case XEN_DOMCTL_ioport_permission:
     case XEN_DOMCTL_ioport_mapping:
+    case XEN_DOMCTL_gsi_permission:
 #endif
 #ifdef CONFIG_HAS_PASSTHROUGH
     /*
Signature · 1 line
2.34.1
#17 lore raw

Re: [RFC XEN PATCH v5 5/5] domctl: Add XEN_DOMCTL_gsi_permission to grant gsi

Quoted 9 lines · Jiqian Chen
tools/libs/ctrl/xc_domain.c +1 -0
index f2d9d14b4d9f..448ba2c59ae1 100644
@@ -1394,6 +1394,21 @@ int xc_domain_irq_permission(xc_interface *xch,
     return do_domctl(xch, &domctl);
 }
 
+int xc_domain_gsi_permission(xc_interface *xch,

tools/libs/ctrl/xc_domain.c int xc_domain_irq_permission(xc_interface *xch, · line 1407

Quoted 10 lines
tools/libs/ctrl/xc_domain.c +10 -0
+                             uint32_t domid,
+                             uint32_t gsi,
+                             bool allow_access)
+{
+    struct xen_domctl domctl = {};
+
+    domctl.cmd = XEN_DOMCTL_gsi_permission;
+    domctl.domain = domid;
+    domctl.u.gsi_permission.gsi = gsi;
+    domctl.u.gsi_permission.allow_access = allow_access;

This could be written as:
struct xen_domctl domctl = {
.cmd = XEN_DOMCTL_gsi_permission, .domain = domid, .u.gsi_permission.gsi = gsi, .u.gsi_permission.allow_access = allow_access,
};

but your change is fine too.

Quoted 12 lines
tools/libs/ctrl/xc_domain.c +4 -0
+
+    return do_domctl(xch, &domctl);
+}
+
 int xc_domain_iomem_permission(xc_interface *xch,
                                uint32_t domid,
                                unsigned long first_mfn,
tools/libs/light/libxl_pci.c +0 -0
index a1c6e82631e9..4136a860a048 100644

tools/libs/light/libxl_pci.c static void pci_add_dm_done(libxl__egc *egc, · line 1425

Quoted 5 lines
tools/libs/light/libxl_pci.c +2 -0
@@ -1421,6 +1421,8 @@ static void pci_add_dm_done(libxl__egc *egc,
     uint32_t flag = XEN_DOMCTL_DEV_RDM_RELAXED;
     uint32_t domainid = domid;
     bool isstubdom = libxl_is_stubdom(ctx, domid, &domainid);
+    int gsi;
+    bool has_gsi = true;

Why is this function has "gsi" variable, and "gsi = irq" but the next function pci_remove_detached() does only have "irq" variable?

So, gsi can only be positive integer, right? Could we forgo has_gsi and just set "gsi = -1" when there's no gsi?

Quoted 11 lines
tools/libs/light/libxl_pci.c +1 -0
     /* Convenience aliases */
     bool starting = pas->starting;
@@ -1482,6 +1484,7 @@ static void pci_add_dm_done(libxl__egc *egc,
                                 pci->bus, pci->dev, pci->func);
 
     if ( access(sysfs_path, F_OK) != 0 ) {
+        has_gsi = false;
         if ( errno == ENOENT )
             sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/irq", pci->domain,
                                 pci->bus, pci->dev, pci->func);

tools/libs/light/libxl_pci.c static void pci_add_dm_done(libxl__egc *egc, · line 1503

Quoted 4 lines
tools/libs/light/libxl_pci.c +1 -0
@@ -1497,6 +1500,7 @@ static void pci_add_dm_done(libxl__egc *egc,
         goto out_no_irq;
     }
     if ((fscanf(f, "%u", &irq) == 1) && irq) {
+        gsi = irq;

Why do you do this, that that mean that gsi and irq are the same? Or does irq happen to sometime contain a gsi value?

Quoted 5 lines
tools/libs/light/libxl_pci.c +0 -0
         r = xc_physdev_map_pirq(ctx->xch, domid, irq, &irq);
         if (r < 0) {
             LOGED(ERROR, domainid, "xc_physdev_map_pirq irq=%d (error=%d)",
@@ -1505,7 +1509,10 @@ static void pci_add_dm_done(libxl__egc *egc,
             rc = ERROR_FAIL;

tools/libs/light/libxl_pci.c static void pci_add_dm_done(libxl__egc *egc, · line 1518

Quoted 10 lines
tools/libs/light/libxl_pci.c +4 -1
             goto out;
         }
-        r = xc_domain_irq_permission(ctx->xch, domid, irq, 1);
+        if ( has_gsi )
+            r = xc_domain_gsi_permission(ctx->xch, domid, gsi, 1);
+        if ( !has_gsi || r == -EOPNOTSUPP )
+            r = xc_domain_irq_permission(ctx->xch, domid, irq, 1);
         if (r < 0) {
             LOGED(ERROR, domainid,
                   "xc_domain_irq_permission irq=%d (error=%d)", irq, r);

Looks like this error message could be wrong, I think we want to know if which call between xc_domain_irq_permission() and xc_domain_gsi_permission() has failed.

Quoted 17 lines
tools/libs/light/libxl_pci.c +2 -0
@@ -2185,6 +2192,7 @@ static void pci_remove_detached(libxl__egc *egc,
     FILE *f;
     uint32_t domainid = prs->domid;
     bool isstubdom;
+    bool has_gsi = true;
 
     /* Convenience aliases */
     libxl_device_pci *const pci = &prs->pci;
@@ -2244,6 +2252,7 @@ skip_bar:
                            pci->bus, pci->dev, pci->func);
 
     if ( access(sysfs_path, F_OK) != 0 ) {
+        has_gsi = false;
         if ( errno == ENOENT )
             sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/irq", pci->domain,
                                 pci->bus, pci->dev, pci->func);

tools/libs/light/libxl_pci.c skip_bar: · line 2283

Quoted 6 lines
tools/libs/light/libxl_pci.c +2 -1
@@ -2270,7 +2279,10 @@ skip_bar:
              */
             LOGED(ERROR, domid, "xc_physdev_unmap_pirq irq=%d", irq);
         }
-        rc = xc_domain_irq_permission(ctx->xch, domid, irq, 0);
+        if ( has_gsi )
+            rc = xc_domain_gsi_permission(ctx->xch, domid, irq, 0);

Why do you use the xc_domain_gsi_permission() with "irq" here, but not in pci_add_dm_done() ?

tools/libs/light/libxl_pci.c skip_bar: · line 2288

Quoted 5 lines
tools/libs/light/libxl_pci.c +2 -0
+        if ( !has_gsi || rc == -EOPNOTSUPP )
+            rc = xc_domain_irq_permission(ctx->xch, domid, irq, 0);
         if (rc < 0) {
             LOGED(ERROR, domid, "xc_domain_irq_permission irq=%d", irq);
         }

These changes to libxl are quite confusing, there's a mixed up between "gsi" and "irq", it's hard to follow.

Thanks,

Signature · 1 line
Anthony PERARD
#18 lore raw

Re: [RFC XEN PATCH v5 5/5] domctl: Add XEN_DOMCTL_gsi_permission to grant gsi

Hi Anthony,

Quoted 21 lines · Anthony PERARD
Quoted 9 lines · Jiqian Chen
tools/libs/ctrl/xc_domain.c +1 -0
index f2d9d14b4d9f..448ba2c59ae1 100644
@@ -1394,6 +1394,21 @@ int xc_domain_irq_permission(xc_interface *xch,
     return do_domctl(xch, &domctl);
 }
 
+int xc_domain_gsi_permission(xc_interface *xch,

tools/libs/ctrl/xc_domain.c int xc_domain_irq_permission(xc_interface *xch, · line 1407

Quoted 10 lines
tools/libs/ctrl/xc_domain.c +10 -0
+                             uint32_t domid,
+                             uint32_t gsi,
+                             bool allow_access)
+{
+    struct xen_domctl domctl = {};
+
+    domctl.cmd = XEN_DOMCTL_gsi_permission;
+    domctl.domain = domid;
+    domctl.u.gsi_permission.gsi = gsi;
+    domctl.u.gsi_permission.allow_access = allow_access;
Quoted 8 lines

This could be written as:
struct xen_domctl domctl = {
.cmd = XEN_DOMCTL_gsi_permission, .domain = domid, .u.gsi_permission.gsi = gsi, .u.gsi_permission.allow_access = allow_access,
};

That's fine, I will change to this in next version.

Quoted 15 lines

but your change is fine too.

Quoted 12 lines
tools/libs/ctrl/xc_domain.c +4 -0
+
+    return do_domctl(xch, &domctl);
+}
+
 int xc_domain_iomem_permission(xc_interface *xch,
                                uint32_t domid,
                                unsigned long first_mfn,
tools/libs/light/libxl_pci.c +0 -0
index a1c6e82631e9..4136a860a048 100644
@@ -1421,6 +1421,8 @@ static void pci_add_dm_done(libxl__egc *egc,
Quoted 8 lines

tools/libs/light/libxl_pci.c static void pci_add_dm_done(libxl__egc *egc, · line 1425

Quoted 5 lines
tools/libs/light/libxl_pci.c +2 -0
     uint32_t flag = XEN_DOMCTL_DEV_RDM_RELAXED;
     uint32_t domainid = domid;
     bool isstubdom = libxl_is_stubdom(ctx, domid, &domainid);
+    int gsi;
+    bool has_gsi = true;

Why is this function has "gsi" variable, and "gsi = irq" but the next function pci_remove_detached() does only have "irq" variable?

Because in pci_add_dm_done, it calls " r = xc_physdev_map_pirq(ctx->xch, domid, irq, &irq); ", it passes the pointer of irq in, and then irq will be changed, so I need to use gsi to save the origin value.

Quoted 3 lines

So, gsi can only be positive integer, right? Could we forgo has_gsi and just set "gsi = -1" when there's no gsi?

No, we can't forgo 'has_gsi', because we need to set gsi = irq to save the original val.

Quoted 6 lines
Quoted 6 lines
tools/libs/light/libxl_pci.c +0 -0
     /* Convenience aliases */
     bool starting = pas->starting;
@@ -1482,6 +1484,7 @@ static void pci_add_dm_done(libxl__egc *egc,
                                 pci->bus, pci->dev, pci->func);
 
Quoted 13 lines

tools/libs/light/libxl_pci.c static void pci_add_dm_done(libxl__egc *egc, · line 1503

Quoted 10 lines
tools/libs/light/libxl_pci.c +2 -0
     if ( access(sysfs_path, F_OK) != 0 ) {
+        has_gsi = false;
         if ( errno == ENOENT )
             sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/irq", pci->domain,
                                 pci->bus, pci->dev, pci->func);
@@ -1497,6 +1500,7 @@ static void pci_add_dm_done(libxl__egc *egc,
         goto out_no_irq;
     }
     if ((fscanf(f, "%u", &irq) == 1) && irq) {
+        gsi = irq;

Why do you do this, that that mean that gsi and irq are the same? Or does irq happen to sometime contain a gsi value?

Above reason.

Quoted 12 lines
Quoted 12 lines
tools/libs/light/libxl_pci.c +3 -1
         r = xc_physdev_map_pirq(ctx->xch, domid, irq, &irq);
         if (r < 0) {
             LOGED(ERROR, domainid, "xc_physdev_map_pirq irq=%d (error=%d)",
@@ -1505,7 +1509,10 @@ static void pci_add_dm_done(libxl__egc *egc,
             rc = ERROR_FAIL;
             goto out;
         }
-        r = xc_domain_irq_permission(ctx->xch, domid, irq, 1);
+        if ( has_gsi )
+            r = xc_domain_gsi_permission(ctx->xch, domid, gsi, 1);
+        if ( !has_gsi || r == -EOPNOTSUPP )
Quoted 8 lines

tools/libs/light/libxl_pci.c static void pci_add_dm_done(libxl__egc *egc, · line 1518

Quoted 4 lines
tools/libs/light/libxl_pci.c +1 -0
+            r = xc_domain_irq_permission(ctx->xch, domid, irq, 1);
         if (r < 0) {
             LOGED(ERROR, domainid,
                   "xc_domain_irq_permission irq=%d (error=%d)", irq, r);

Looks like this error message could be wrong, I think we want to know if which call between xc_domain_irq_permission() and xc_domain_gsi_permission() has failed.

You are right, I will separate them in next version.

Quoted 12 lines
Quoted 12 lines
tools/libs/light/libxl_pci.c +1 -0
@@ -2185,6 +2192,7 @@ static void pci_remove_detached(libxl__egc *egc,
     FILE *f;
     uint32_t domainid = prs->domid;
     bool isstubdom;
+    bool has_gsi = true;
 
     /* Convenience aliases */
     libxl_device_pci *const pci = &prs->pci;
@@ -2244,6 +2252,7 @@ skip_bar:
                            pci->bus, pci->dev, pci->func);
 
Quoted 15 lines

tools/libs/light/libxl_pci.c skip_bar: · line 2283

Quoted 12 lines
tools/libs/light/libxl_pci.c +3 -1
     if ( access(sysfs_path, F_OK) != 0 ) {
+        has_gsi = false;
         if ( errno == ENOENT )
             sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/irq", pci->domain,
                                 pci->bus, pci->dev, pci->func);
@@ -2270,7 +2279,10 @@ skip_bar:
              */
             LOGED(ERROR, domid, "xc_physdev_unmap_pirq irq=%d", irq);
         }
-        rc = xc_domain_irq_permission(ctx->xch, domid, irq, 0);
+        if ( has_gsi )
+            rc = xc_domain_gsi_permission(ctx->xch, domid, irq, 0);

Why do you use the xc_domain_gsi_permission() with "irq" here, but not in pci_add_dm_done() ?

Because xc_physdev_unmap_pirq doesn't change the value of irq, but in pci_add_dm_done, the value of irq is changed by xc_physdev_map_pirq.

Quoted 13 lines

tools/libs/light/libxl_pci.c skip_bar: · line 2288

Quoted 6 lines
tools/libs/light/libxl_pci.c +2 -0
+        if ( !has_gsi || rc == -EOPNOTSUPP )
+            rc = xc_domain_irq_permission(ctx->xch, domid, irq, 0);
         if (rc < 0) {
             LOGED(ERROR, domid, "xc_domain_irq_permission irq=%d", irq);
         }

These changes to libxl are quite confusing, there's a mixed up between "gsi" and "irq", it's hard to follow.

Thanks,

Signature · 2 lines
Best regards,
Jiqian Chen.
#19 lore raw

Re: [RFC XEN PATCH v5 5/5] domctl: Add XEN_DOMCTL_gsi_permission to grant gsi

Quoted 15 lines · Jiqian
Quoted 14 lines · Anthony PERARD

tools/libs/light/libxl_pci.c static void pci_add_dm_done(libxl__egc *egc, · line 1425

Quoted 10 lines · Jiqian Chen
tools/libs/light/libxl_pci.c +2 -0
index a1c6e82631e9..4136a860a048 100644
@@ -1421,6 +1421,8 @@ static void pci_add_dm_done(libxl__egc *egc,
     uint32_t flag = XEN_DOMCTL_DEV_RDM_RELAXED;
     uint32_t domainid = domid;
     bool isstubdom = libxl_is_stubdom(ctx, domid, &domainid);
+    int gsi;
+    bool has_gsi = true;

Why is this function has "gsi" variable, and "gsi = irq" but the next function pci_remove_detached() does only have "irq" variable?

Quoted 1 line

Because in pci_add_dm_done, it calls " r = xc_physdev_map_pirq(ctx->xch, domid, irq, &irq); ", it passes the pointer of irq in, and then irq will be changed, so I need to use gsi to save the origin value.

I'm sorry but unconditional "gsi = irq" looks very wrong, that line needs to be removed or changed, otherwise we have two functions doing the same thing just after that (xc_domain_irq_permission and xc_domain_gsi_permission). Instead, my guess is that the arguments of xc_physdev_map_pirq() needs to be changes. What does contain irq after the map_pirq() call? A "pirq" of some kind? Maybe xc_physdev_map_pirq(ctx->xch, domid, irq, &pirq) would make things more clear about what's going on.

And BTW, maybe renaming the variable "has_gsi" to "is_gsi" might make things a bit clearer as well, as in: "the variable 'irq' $is_gsi", instead of a variable that have a meaning of "the system $has_gsi" without necessarily meaning that the code is using it.

Maybe using (abusing?) the variable name "irq" might be a bit wrong now? What does "GSI" stand for anyway? And about "PIRQ"? This is just some question to figure out if there's potentially a better name for the variables names.

Thanks,

Signature · 1 line
Anthony PERARD
#20 lore raw

Re: [RFC XEN PATCH v5 5/5] domctl: Add XEN_DOMCTL_gsi_permission to grant gsi

Quoted 16 lines · Anthony PERARD
Quoted 15 lines · Jiqian
Quoted 14 lines · Anthony PERARD

tools/libs/light/libxl_pci.c static void pci_add_dm_done(libxl__egc *egc, · line 1425

Quoted 10 lines · Jiqian Chen
tools/libs/light/libxl_pci.c +2 -0
index a1c6e82631e9..4136a860a048 100644
@@ -1421,6 +1421,8 @@ static void pci_add_dm_done(libxl__egc *egc,
     uint32_t flag = XEN_DOMCTL_DEV_RDM_RELAXED;
     uint32_t domainid = domid;
     bool isstubdom = libxl_is_stubdom(ctx, domid, &domainid);
+    int gsi;
+    bool has_gsi = true;

Why is this function has "gsi" variable, and "gsi = irq" but the next function pci_remove_detached() does only have "irq" variable?

Quoted 3 lines
Quoted 1 line

Because in pci_add_dm_done, it calls " r = xc_physdev_map_pirq(ctx->xch, domid, irq, &irq); ", it passes the pointer of irq in, and then irq will be changed, so I need to use gsi to save the origin value.

I'm sorry but unconditional "gsi = irq" looks very wrong, that line

Make sense, I should add a condition and some comments here. Maybe:

	/* if has gsi sysfs node, then fscanf() read from the gsi sysfs and store it in irq variable, we need to save the original gsi value, because irq will be changed in xc_physdev_map_pirq */
	if (has_gsi)
		gsi = irq;
Quoted 5 lines

needs to be removed or changed, otherwise we have two functions doing the same thing just after that (xc_domain_irq_permission and xc_domain_gsi_permission). Instead, my guess is that the arguments of xc_physdev_map_pirq() needs to be changes. What does contain irq after the map_pirq() call? A "pirq" of some kind? Maybe

Yes, pirq.

Quoted 8 lines

xc_physdev_map_pirq(ctx->xch, domid, irq, &pirq) would make things more clear about what's going on.

And BTW, maybe renaming the variable "has_gsi" to "is_gsi" might make things a bit clearer as well, as in: "the variable 'irq' $is_gsi", instead of a variable that have a meaning of "the system $has_gsi" without necessarily meaning that the code is using it.

Gsi is a new sysfs node added by my kernel patch, and it is still in discussion with PCI Maintainer. And for compatible with old version of kernel that didn't has gsi sysfs node, we still need to use irq here. So, I named this variable to has_gsi. Is it clearer that changing it to has_gsi_sysfs_node? Maybe I need to add some comments in code to make the usage of gsi clear.

Quoted 3 lines

Maybe using (abusing?) the variable name "irq" might be a bit wrong now? What does "GSI" stand for anyway? And about "PIRQ"? This is just some

GSI is x86 specific concept, it is related to IOAPIC-PIN. PIRQ is used to route interrupts in Xen.

Quoted 5 lines

question to figure out if there's potentially a better name for the variables names.

Thanks,

Signature · 2 lines
Best regards,
Jiqian Chen.