Skip to contentSkip to conversation

pci: a workaround for nonstandard PCI devices whose PBA shares

PATCH In review Chao Gao 2 participants 4 messages Apr 9, 2018

lore
Share & tools
lore

Chao Gao

author
#0 lore raw

PATCH v2 1/1 pci: a workaround for nonstandard PCI devices whose PBA shares

... the same page with other registers which are not relevant to MSI-X. Xen marks pages where PBA resides as read-only. When assigning such devices to guest, device driver writes MSI-X irrelevant registers on those pages would lead to an EPT violation and the guest is destroyed because no handler is registered for those address range. In order to make guest capable to use such kind of devices, trapping very frequent write accesses is not a good idea for it would significantly impact the performance.

This patch provides a workaround with caveat. Specifically, an option is introduced to specify a list of devices. For those devices, Xen doesn't control the access right to pages where PBA resides. Hence, guest device driver is able to write those pages and functions well. Note that adding an untrusted device to this option may endanger security of the entire system.

---
Given that parsing parameters starts at very early stage in which xmalloc is unusable, I choose to continue using an array other than a list to store SBDFs of such kind devices, like the way how we manage phantom_devs.

changes in v2:
- use a more informative name, pba_shared_quirk, to describe the quirk
- use pci_sbdf_t other than uint32_t
- disable this quirk when PBA shares the same page with other RO mmio
- exit the loop in alloc_pdev() once a match is found

---
 docs/misc/xen-command-line.markdown | 10 ++++++++
 xen/arch/x86/msi.c                  | 20 ++++++++++++++--
 xen/drivers/passthrough/pci.c       | 48 +++++++++++++++++++++++++++++++++++--
 xen/include/asm-x86/msi.h           |  1 +
 4 files changed, 75 insertions(+), 4 deletions(-)
docs/misc/xen-command-line.markdown +10 -0
index b353352..7c10e63 100644
@@ -1423,6 +1423,16 @@ Defaults to booting secondary processors.
 
 > Default: `on`
 
+### pba\_shared\_quirk
+> `= List of [<seg>:]<bus>:<device>.<function>`
+
+Specify a list of SBDF of devices. When assigning devices in this list
+to guest, reading or writing the page where MSI-X PBA resides are
+allowed. This option provides a workaround for nonstandard PCI devices
+whose MSI-X PBA shares the same 4K-byte page with registers irrelevant
+to MSI-X. Note that adding an untrusted device to this option would
+undermine security of the entire system.
+
 ### pci
 > `= {no-}serr | {no-}perr`
 
xen/arch/x86/msi.c +18 -2
index 5567990..53280ec 100644
@@ -992,7 +992,22 @@ static int msix_capability_init(struct pci_dev *dev,
         if ( rangeset_add_range(mmio_ro_ranges, msix->table.first,
                                 msix->table.last) )
             WARN();
-        if ( rangeset_add_range(mmio_ro_ranges, msix->pba.first,
+
+        /*
+         * If pages where MSI-X PBA resides overlap with other read-only mmio
+         * range, pba_shared_quirk won't meet our desire. Hence disable it.
+         */
+        if ( msix->pba_shared_quirk_enabled &&
+             rangeset_overlaps_range(mmio_ro_ranges, msix->pba.first,
+                                     msix->pba.last) )
+        {
+            printk("PBA_shared_quirk is disabled for %04x:%02x:%02x.%u",
+                   seg, bus, slot, func);
+            msix->pba_shared_quirk_enabled = false;
+        }
+
+        if ( !msix->pba_shared_quirk_enabled &&
+             rangeset_add_range(mmio_ro_ranges, msix->pba.first,
                                 msix->pba.last) )
             WARN();
 
@@ -1139,7 +1154,8 @@ static void _pci_cleanup_msix(struct arch_msix *msix)
         if ( rangeset_remove_range(mmio_ro_ranges, msix->table.first,
                                    msix->table.last) )
             WARN();
-        if ( rangeset_remove_range(mmio_ro_ranges, msix->pba.first,
+        if ( !msix->pba_shared_quirk_enabled &&
+             rangeset_remove_range(mmio_ro_ranges, msix->pba.first,
                                    msix->pba.last) )
             WARN();
     }
xen/drivers/passthrough/pci.c +46 -2

72 lines not on this page · raw message

xen/include/asm-x86/msi.h +1 -0
index 10387dc..039cce7 100644
@@ -239,6 +239,7 @@ struct arch_msix {
     int table_idx[MAX_MSIX_TABLE_PAGES];
     spinlock_t table_lock;
     bool host_maskall, guest_maskall;
+    bool pba_shared_quirk_enabled;
     domid_t warned;
 };
 
Signature · 7 lines
1.8.3.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 09.04.18 at 15:16, <[email protected]> wrote:

Given that parsing parameters starts at very early stage in which xmalloc is unusable, I choose to continue using an array other than a list to store SBDFs of such kind devices, like the way how we manage phantom_devs.

Yes, I was about to say that on v1 when I saw v2 come in.

Quoted 5 lines
docs/misc/xen-command-line.markdown +0 -0
@@ -1423,6 +1423,16 @@ Defaults to booting secondary processors.
 
 > Default: `on`

docs/misc/xen-command-line.markdown Defaults to booting secondary processors. · line 1434

Quoted 10 lines
docs/misc/xen-command-line.markdown +9 -0
 
+### pba\_shared\_quirk
+> `= List of [<seg>:]<bus>:<device>.<function>`
+
+Specify a list of SBDF of devices. When assigning devices in this list
+to guest, reading or writing the page where MSI-X PBA resides are
+allowed. This option provides a workaround for nonstandard PCI devices
+whose MSI-X PBA shares the same 4K-byte page with registers irrelevant
+to MSI-X. Note that adding an untrusted device to this option would
+undermine security of the entire system.

No underscores in new command line options please - use dashes.

I'm not convinced though that a global option is well suited here:
Exposing the device in this way may be okay for one guest, but not for another. With your model one would need to reboot the entire host for such a usage change.

Furthermore boot time specification of SBDF is a problem with the Dom0 kernel possibly re-organizing the topology, and is not going to work well with hot-plugged devices.

IOW - I think this setting needs to be specified at device assignment time.

Quoted 27 lines
xen/arch/x86/msi.c +16 -1
@@ -992,7 +992,22 @@ static int msix_capability_init(struct pci_dev *dev,
         if ( rangeset_add_range(mmio_ro_ranges, msix->table.first,
                                 msix->table.last) )
             WARN();
-        if ( rangeset_add_range(mmio_ro_ranges, msix->pba.first,
+
+        /*
+         * If pages where MSI-X PBA resides overlap with other read-only mmio
+         * range, pba_shared_quirk won't meet our desire. Hence disable it.
+         */
+        if ( msix->pba_shared_quirk_enabled &&
+             rangeset_overlaps_range(mmio_ro_ranges, msix->pba.first,
+                                     msix->pba.last) )
+        {
+            printk("PBA_shared_quirk is disabled for %04x:%02x:%02x.%u",
+                   seg, bus, slot, func);
+            msix->pba_shared_quirk_enabled = false;
+        }
+
+        if ( !msix->pba_shared_quirk_enabled &&
+             rangeset_add_range(mmio_ro_ranges, msix->pba.first,
                                 msix->pba.last) )
             WARN();
 

xen/arch/x86/msi.c static void _pci_cleanup_msix(struct arch_msix *msix) · line 1159

Quoted 7 lines
xen/arch/x86/msi.c +2 -1
@@ -1139,7 +1154,8 @@ static void _pci_cleanup_msix(struct arch_msix *msix)
         if ( rangeset_remove_range(mmio_ro_ranges, msix->table.first,
                                    msix->table.last) )
             WARN();
-        if ( rangeset_remove_range(mmio_ro_ranges, msix->pba.first,
+        if ( !msix->pba_shared_quirk_enabled &&
+             rangeset_remove_range(mmio_ro_ranges, msix->pba.first,
                                    msix->pba.last) )

I don't think you need to alter the condition here.

xen/drivers/passthrough/pci.c static int __init parse_phantom_dev(const char *str) · line 189

Quoted 9 lines
xen/drivers/passthrough/pci.c +3 -0
@@ -184,6 +184,39 @@ static int __init parse_phantom_dev(const char *str)
 }
 custom_param("pci-phantom", parse_phantom_dev);
 
+static struct pba_shared_quirk_dev {
+    pci_sbdf_t sbdf;
+} pba_shared_quirk_devs[8];

Any reason this can't be of type pci_sbdf_t[8]?

xen/drivers/passthrough/pci.c static int __init parse_phantom_dev(const char *str) · line 190

Quoted 1 line
xen/drivers/passthrough/pci.c +1 -0
+static unsigned int nr_pba_shared_quirk_devs;

Both __read_mostly please.

xen/include/asm-x86/msi.h struct arch_msix { · line 242

Quoted 7 lines
xen/include/asm-x86/msi.h +1 -0
@@ -239,6 +239,7 @@ struct arch_msix {
     int table_idx[MAX_MSIX_TABLE_PAGES];
     spinlock_t table_lock;
     bool host_maskall, guest_maskall;
+    bool pba_shared_quirk_enabled;

I don't think you need the "_enabled" suffix - it's a boolean after all.

Jan

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

author
#2 lore raw
Quoted 13 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 09.04.18 at 15:16, <[email protected]> wrote:

Given that parsing parameters starts at very early stage in which xmalloc is unusable, I choose to continue using an array other than a list to store SBDFs of such kind devices, like the way how we manage phantom_devs.

Yes, I was about to say that on v1 when I saw v2 come in.

Quoted 6 lines
docs/misc/xen-command-line.markdown +0 -0
@@ -1423,6 +1423,16 @@ Defaults to booting secondary processors.
 
 > Default: `on`
 
Quoted 11 lines

docs/misc/xen-command-line.markdown Defaults to booting secondary processors. · line 1434

Quoted 9 lines
docs/misc/xen-command-line.markdown +9 -0
+### pba\_shared\_quirk
+> `= List of [<seg>:]<bus>:<device>.<function>`
+
+Specify a list of SBDF of devices. When assigning devices in this list
+to guest, reading or writing the page where MSI-X PBA resides are
+allowed. This option provides a workaround for nonstandard PCI devices
+whose MSI-X PBA shares the same 4K-byte page with registers irrelevant
+to MSI-X. Note that adding an untrusted device to this option would
+undermine security of the entire system.

No underscores in new command line options please - use dashes.

Will do.

Quoted 9 lines

I'm not convinced though that a global option is well suited here:
Exposing the device in this way may be okay for one guest, but not for another. With your model one would need to reboot the entire host for such a usage change.

Furthermore boot time specification of SBDF is a problem with the Dom0 kernel possibly re-organizing the topology, and is not going to work well with hot-plugged devices.

Point taken. But one concern is if we don't specify devices at boot time whether PVH dom0 can use such devices (in my understanding, PVH dom0 will use EPT, it will meet the same issue as HVM domU). If not, we need treat dom0 as an exception; pages where PBA resides won't be marked as read-only. But mmio_ro_ranges is global. Maybe build two separate mmio_ro_ranges for dom0 and domU?

Thanks
Chao

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

#3 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 09.04.18 at 16:10, <[email protected]> wrote:
Quoted 8 lines · Jan Beulich

I'm not convinced though that a global option is well suited here:
Exposing the device in this way may be okay for one guest, but not for another. With your model one would need to reboot the entire host for such a usage change.

Furthermore boot time specification of SBDF is a problem with the Dom0 kernel possibly re-organizing the topology, and is not going to work well with hot-plugged devices.

Quoted 6 lines

Point taken. But one concern is if we don't specify devices at boot time whether PVH dom0 can use such devices (in my understanding, PVH dom0 will use EPT, it will meet the same issue as HVM domU). If not, we need treat dom0 as an exception; pages where PBA resides won't be marked as read-only. But mmio_ro_ranges is global. Maybe build two separate mmio_ro_ranges for dom0 and domU?

Boot time Dom0 settings can come from the command line. But already for hot-plugged devices (which go to Dom0 as well) this approach would break. Generally permitting Dom0 write access here is certainly an option, but strictly only as long as the MSI-X table won't also become writable.

Jan

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