Yet another pci/vpci locking re-work
cover Yet another pci/vpci locking re-work
From: Oleksandr Andrushchenko <[email protected]>
Hello, all!
This is a yet another attempt to re-work the existing pci/vpci locking scheme towards vPCI is going to be used for guests.
For more details on the previous attempts and their flaws please see [1], [2].
This work is based on the idea that it is possible to extend the existing locking scheme by additionally providing a global read/write lock:
This way most of the code continues to use pcidevs_{lock|unlock}, so only minor changes are required which do not lead to functional changes seen by the users: these become readers with respect to the new rwlock and they acquire _pcidevs_lock as before.
As to the writers (those which can add/remove pci devices and their respective pdev) we need to make them use the new rwlock in write mode. For that we introduce pcidevs_write_{un}lock helpers.
Those users, which do not add/remove pdevs and are only interested in pdev->vpci or simple pdev's list traversal, will use pcidevs_read_{lock|unlock} which only acquires the rwlock in read mode without acquiring _pcidevs_lock. This is to make sure there is no unnecessary contention for the later. For the cases when it is required to make sure that no other CPU can access critical sections under the read lock (modify_bars for example) it is possible to upgrade it to a normal pcidevs_lock due to both read lock and _pcidevs_lock allow recursion.
The series was tested on:
- x86 PVH Dom0 and doesn't break it.
- x86 HVM with PCI passthrough to DomU and doesn't break it.
Thank you,
Oleksandr
[1] https://lore.kernel.org/xen-devel/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
Oleksandr Andrushchenko (4):
pci: add rwlock to pcidevs_lock machinery
vpci: restrict unhandled read/write operations for guests
vpci: use pcidevs locking to protect MMIO handlers
vpci: resolve possible clash while removing BAR overlaps
xen/arch/x86/hvm/vmsi.c | 24 +++++++++++-- xen/drivers/passthrough/pci.c | 65 +++++++++++++++++++++++++++++------ xen/drivers/vpci/header.c | 30 ++++++++++++++-- xen/drivers/vpci/msi.c | 21 +++++++---- xen/drivers/vpci/msix.c | 55 +++++++++++++++++++++++++---- xen/drivers/vpci/vpci.c | 34 +++++++++++++++--- xen/include/xen/pci.h | 9 +++++ xen/include/xen/vpci.h | 2 +- 8 files changed, 205 insertions(+), 35 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/4 pci: add rwlock to pcidevs_lock machinery
From: Oleksandr Andrushchenko <[email protected]>
Currently pcidevs lock is a global recursive spinlock which is fine for the existing use cases. It is used to both protect pdev instances themselves from being removed while in use and to make sure the update of the relevant pdev properties is synchronized.
Moving towards vPCI is used for guests this becomes problematic in terms of lock contention. For example, during vpci_{read|write} the access to pdev must be protected to prevent pdev disappearing under our feet. This needs to be done with the help of pcidevs_{lock|unlock}. On the other hand it is highly undesirable to lock all other pdev accesses which only use pdevs in read mode, e.g. those which do not remove or add pdevs.
For the above reasons introduce a read/write lock which will help preventing locking contentions between pdev readers and writers:
- make pci_{add|remove}_device and setup_hwdom_pci_devices use the new write lock
- keep all the rest using the existing API (pcidevs_{lock|unlock}, but extend the later to also acquire the rwlock in read mode.
This is in preparation for vPCI to be used for guests.
--- xen/drivers/passthrough/pci.c | 45 ++++++++++++++++++++++++++--------- xen/include/xen/pci.h | 4 ++++ 2 files changed, 38 insertions(+), 11 deletions(-)
xen/drivers/passthrough/pci.c +34 -11
@@ -51,20 +51,38 @@ struct pci_seg { };static spinlock_t _pcidevs_lock =SPIN_LOCK_UNLOCKED ; +static DEFINE_RWLOCK (_pcidevs_rwlock);void pcidevs_lock (void ) { +read_lock (&_pcidevs_rwlock);spin_lock_recursive (&_pcidevs_lock); }void pcidevs_unlock (void ) {spin_unlock_recursive (&_pcidevs_lock); +read_unlock (&_pcidevs_rwlock); }bool_t pcidevs_locked (void ) { -return !!spin_is_locked (&_pcidevs_lock); +return !!spin_is_locked (&_pcidevs_lock) ||pcidevs_write_locked (); +} + +void pcidevs_write_lock (void ) +{ +write_lock (&_pcidevs_rwlock); +} + +void pcidevs_write_unlock (void ) +{ +write_unlock (&_pcidevs_rwlock); +} + +bool pcidevs_write_locked (void ) +{ +return !!rw_is_write_locked (&_pcidevs_rwlock); }static struct radix_tree_root pci_segments;
@@ -758,7 +776,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn, ret = -ENOMEM ; -pcidevs_lock (); +pcidevs_write_lock (); pseg =alloc_pseg (seg);if ( !pseg ) goto out;
@@ -854,7 +872,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,pci_enable_acs (pdev); out: -pcidevs_unlock (); +pcidevs_write_unlock ();if ( !ret ) {printk (XENLOG_DEBUG "PCI add %s %pp\n" , pdev_type, &pdev->sbdf);
@@ -885,7 +903,7 @@ int pci_remove_device(u16 seg, u8 bus, u8 devfn)if ( !pseg )return -ENODEV ; -pcidevs_lock (); +pcidevs_write_lock (); list_for_each_entry ( pdev, &pseg->alldevs_list, alldevs_list )if ( pdev->bus == bus && pdev->devfn == devfn ) {
@@ -899,7 +917,7 @@ int pci_remove_device(u16 seg, u8 bus, u8 devfn)break ; } -pcidevs_unlock (); +pcidevs_write_unlock ();return ret; }
@@ -1176,6 +1194,11 @@ static void __hwdom_init setup_one_hwdom_device(const struct setup_hwdom *ctxt, ctxt->d->domain_id, err); } +/* +* It's safe to drop and re-acquire the write lock in this context without +* risking pdev disappearing because devices cannot be removed until the +* initial domain has been started. +*/ static int __hwdom_init _setup_hwdom_pci_devices(struct pci_seg *pseg, void *arg) {struct setup_hwdom *ctxt = arg;
@@ -1208,17 +1231,17 @@ static int __hwdom_init _setup_hwdom_pci_devices(struct pci_seg *pseg, void *argif ( iommu_verbose ) { -pcidevs_unlock (); +pcidevs_write_unlock ();process_pending_softirqs (); -pcidevs_lock (); +pcidevs_write_lock (); } }if ( !iommu_verbose ) { -pcidevs_unlock (); +pcidevs_write_unlock ();process_pending_softirqs (); -pcidevs_lock (); +pcidevs_write_lock (); } }
@@ -1230,9 +1253,9 @@ void __hwdom_init setup_hwdom_pci_devices( {struct setup_hwdom ctxt = { .d = d, .handler = handler }; -pcidevs_lock (); +pcidevs_write_lock ();pci_segments_iterate (_setup_hwdom_pci_devices, &ctxt); -pcidevs_unlock (); +pcidevs_write_unlock (); }/* APEI not supported on ARM yet. */
xen/include/xen/pci.h +4 -0
@@ -152,6 +152,10 @@ void pcidevs_lock(void);void pcidevs_unlock (void );bool_t __must_check pcidevs_locked (void ); +void pcidevs_write_lock (void ); +void pcidevs_write_unlock (void ); +bool __must_check pcidevs_write_locked (void ); +bool_t pci_known_segment (u16 seg);bool_t pci_device_detect (u16 seg,u8 bus,u8 dev,u8 func);int scan_pci_devices (void );
Signature · 1 line
2.25.1
PATCH 2/4 vpci: restrict unhandled read/write operations for guests
In reply to Oleksandr AndrushchenkoFrom: Oleksandr Andrushchenko <[email protected]>
A guest would be able to read and write those registers which are not emulated and have no respective vPCI handlers, so it will be possible for it to access the hardware directly.
In order to prevent a guest from reads and writes from/to the unhandled registers make sure only hardware domain can access the hardware directly and restrict guests from doing so.
---
Since v6:
- do not use is_hwdom parameter for vpci_{read|write}_hw and use current->domain internally
- update commit message
New in v6
--- xen/drivers/vpci/vpci.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
xen/drivers/vpci/vpci.c +10 -2
@@ -213,6 +213,10 @@ static uint32_t vpci_read_hw(pci_sbdf_t sbdf, unsigned int reg, {uint32_t data; +/* Guest domains are not allowed to read real hardware. */ +if ( !is_hardware_domain (current->domain) ) +return ~(uint32_t )0 ; +switch ( size ) {case 4 :
@@ -253,9 +257,13 @@ static uint32_t vpci_read_hw(pci_sbdf_t sbdf, unsigned int reg,return data; } -static void vpci_write_hw (pci_sbdf_t sbdf,unsigned int reg,unsigned int size, -uint32_t data) +static void vpci_write_hw (pci_sbdf_t sbdf,unsigned int reg, +unsigned int size,uint32_t data) { +/* Guest domains are not allowed to write real hardware. */ +if ( !is_hardware_domain (current->domain) ) +return ; +switch ( size ) {case 4 :
Signature · 1 line
2.25.1
PATCH 4/4 vpci: resolve possible clash while removing BAR overlaps
In reply to Oleksandr AndrushchenkoFrom: Oleksandr Andrushchenko <[email protected]>
modify_bars checks if the mapping of the BAR memory has already been done when mapping other device's BARs or, while unmapping, are still in use by other devices.
With the existing locking scheme it is possible that there are other devices trying to do the same in parallel with us, but on other CPUs as we only hold a read lock without acquiring _pcidevs_lock recursive lock.
To prevent that upgrade the read lock to normal pcidevs_lock during BAR overlapping check.
--- xen/drivers/vpci/header.c | 6 ++++++ 1 file changed, 6 insertions(+)
xen/drivers/vpci/header.c +6 -0
@@ -281,7 +281,11 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)/* * Check for overlaps with other BARs. Note that only BARs that are * currently mapped (enabled) are checked for overlaps. +* We are holding pcidevs_read_lock here, but we need to access +* different devices at a time. So, upgrade our current read lock to normal +* pcidevs_lock. */ +pcidevs_lock (); for_each_pdev ( pdev->domain, tmp ) {if ( tmp == pdev )
@@ -321,10 +325,12 @@ static int modify_bars(const struct pci_dev *pdev, uint16_t cmd, bool rom_only)printk (XENLOG_G_WARNING "Failed to remove [%lx, %lx]: %d\n" , start, end, rc);rangeset_destroy (mem); +pcidevs_unlock ();return rc; } } } +pcidevs_unlock ();ASSERT (dev);
Signature · 1 line
2.25.1
PATCH 3/4 vpci: use pcidevs locking to protect MMIO handlers
In reply to Oleksandr AndrushchenkoFrom: Oleksandr Andrushchenko <[email protected]>
vPCI MMIO handlers are accessing pdevs without protecting this access with pcidevs_{lock|unlock}. This is not a problem as of now as these are only used by Dom0. But, towards vPCI is used also for guests, we need to properly protect pdev and pdev->vpci from being removed while still in use.
For that add new locking helpers: pcidevs_read_{un}lock and means to check if the lock is held in read mode.
Note, that pcidevs_read_{un}lock doesn't acquire _pcidevs_lock recursive lock because its users are not expected to modify pdev's contents other than pdev->vpci which is protected by pdev->vpci->lock (where appropriate). These new helpers are also suitable for simple pdev list traversals such as for_each_pdev, pci_get_pdev_by_domain and others.
This patch adds ASSERTs in the code to check that the rwlock is taken and in appropriate mode. Some of such checks require changes to the initialization of local variables which may be accessed before the ASSERT checks the locking. For example see init_bars and mask_write.
--- xen/arch/x86/hvm/vmsi.c | 24 +++++++++++++-- xen/drivers/passthrough/pci.c | 20 +++++++++++++ xen/drivers/vpci/header.c | 24 +++++++++++++-- xen/drivers/vpci/msi.c | 21 +++++++++---- xen/drivers/vpci/msix.c | 55 ++++++++++++++++++++++++++++++----- xen/drivers/vpci/vpci.c | 22 ++++++++++++-- xen/include/xen/pci.h | 5 ++++ xen/include/xen/vpci.h | 2 +- 8 files changed, 151 insertions(+), 22 deletions(-)
xen/arch/x86/hvm/vmsi.c +21 -3
@@ -889,10 +889,16 @@ void vpci_msix_arch_init_entry(struct vpci_msix_entry *entry) entry->arch.pirq =INVALID_PIRQ ; } -int vpci_msix_arch_print (const struct vpci_msix *msix) +int vpci_msix_arch_print (const struct domain *d,const struct vpci_msix *msix) {unsigned int i; +/* +* FIXME: this is not immediately correct, as the lock can be grabbed +* by a different CPU. But this is better then nothing. +*/ +ASSERT (pcidevs_read_locked ()); +for ( i =0 ; i < msix->max_entries; i++ ) {const struct vpci_msix_entry *entry = &msix->entries[i];
@@ -909,11 +915,23 @@ int vpci_msix_arch_print(const struct vpci_msix *msix)if ( i && !(i %64 ) ) {struct pci_dev *pdev = msix->pdev; +pci_sbdf_t sbdf = pdev->sbdf;spin_unlock (&msix->pdev->vpci->lock); +pcidevs_read_unlock (); + +/* NB: we still hold rcu_read_lock(&domlist_read_lock); here. */ process_pending_softirqs (); -/* NB: we assume that pdev cannot go away for an alive domain. */ -if ( !pdev->vpci || !spin_trylock (&pdev->vpci->lock) ) + +if ( !pcidevs_read_trylock () ) +return -EBUSY ; + pdev =pci_get_pdev_by_domain (d, sbdf.seg, sbdf.bus, sbdf.devfn); +/* +* FIXME: we may find a re-allocated pdev's copy here. +* Even occupying the same address as before. Do our best. +*/ +if ( !pdev || (pdev != msix->pdev) || !pdev->vpci || + !spin_trylock (&pdev->vpci->lock) )return -EBUSY ;if ( pdev->vpci->msix != msix ) {
xen/drivers/passthrough/pci.c +20 -0
@@ -70,6 +70,26 @@ bool_t pcidevs_locked(void)return !!spin_is_locked (&_pcidevs_lock) ||pcidevs_write_locked (); } +void pcidevs_read_lock (void ) +{ +read_lock (&_pcidevs_rwlock); +} + +int pcidevs_read_trylock (void ) +{ +return read_trylock (&_pcidevs_rwlock); +} + +void pcidevs_read_unlock (void ) +{ +read_unlock (&_pcidevs_rwlock); +} + +bool pcidevs_read_locked (void ) +{ +return !!rw_is_locked (&_pcidevs_rwlock); +} +void pcidevs_write_lock (void ) {write_lock (&_pcidevs_rwlock);
xen/drivers/vpci/header.c +22 -2
@@ -142,16 +142,19 @@ bool vpci_process_pending(struct vcpu *v)if ( rc == -ERESTART )return true; +pcidevs_read_lock ();spin_lock (&v->vpci.pdev->vpci->lock);/* Disable memory decoding unconditionally on failure. */ modify_decoding (v->vpci.pdev, rc ? v->vpci.cmd & ~PCI_COMMAND_MEMORY : v->vpci.cmd, !rc && v->vpci.rom_only);spin_unlock (&v->vpci.pdev->vpci->lock); +pcidevs_read_unlock ();rangeset_destroy (v->vpci.mem); v->vpci.mem =NULL ;if ( rc ) + {/* * FIXME: in case of failure remove the device from the domain. * Note that there might still be leftover mappings. While this is
@@ -159,7 +162,10 @@ bool vpci_process_pending(struct vcpu *v)* killed in order to avoid leaking stale p2m mappings on * failure. */ +pcidevs_write_lock ();vpci_remove_device (v->vpci.pdev); +pcidevs_write_unlock (); + } }return false;
@@ -172,7 +178,16 @@ static int __init apply_map(struct domain *d, const struct pci_dev *pdev,int rc;while ( (rc =rangeset_consume_ranges (mem, map_range, &data)) == -ERESTART ) + { +/* +* It's safe to drop and re-acquire the lock in this context +* without risking pdev disappearing because devices cannot be +* removed until the initial domain has been started. +*/ +pcidevs_write_unlock ();process_pending_softirqs (); +pcidevs_write_lock (); + }rangeset_destroy (mem);if ( !rc )modify_decoding (pdev, cmd, false);
@@ -450,10 +465,15 @@ static int init_bars(struct pci_dev *pdev)uint16_t cmd;uint64_t addr, size;unsigned int i, num_bars, rom_reg; -struct vpci_header *header = &pdev->vpci->header; -struct vpci_bar *bars = header->bars; +struct vpci_header *header; +struct vpci_bar *bars;int rc; +ASSERT (pcidevs_write_locked ()); + + header = &pdev->vpci->header; + bars = header->bars; +switch (pci_conf_read8 (pdev->sbdf,PCI_HEADER_TYPE ) &0x7f ) {case PCI_HEADER_TYPE_NORMAL :
xen/drivers/vpci/msi.c +15 -6
@@ -184,12 +184,17 @@ static void mask_write(const struct pci_dev *pdev, unsigned int reg,static int init_msi (struct pci_dev *pdev) { -uint8_t slot =PCI_SLOT (pdev->devfn), func =PCI_FUNC (pdev->devfn); -unsigned int pos =pci_find_cap_offset (pdev->seg, pdev->bus, slot, func, -PCI_CAP_ID_MSI ); +uint8_t slot, func; +unsigned int pos;uint16_t control;int ret; +ASSERT (pcidevs_write_locked ()); + + slot =PCI_SLOT (pdev->devfn); + func =PCI_FUNC (pdev->devfn); + pos =pci_find_cap_offset (pdev->seg, pdev->bus, slot, func,PCI_CAP_ID_MSI ); +if ( !pos )return 0 ;
@@ -277,6 +282,9 @@ void vpci_dump_msi(void)printk ("vPCI MSI/MSI-X d%d\n" , d->domain_id); +if ( !pcidevs_read_trylock () ) +continue ; + for_each_pdev ( d, pdev ) {const struct vpci_msi *msi;
@@ -310,7 +318,7 @@ void vpci_dump_msi(void)printk (" entries: %u maskall: %d enabled: %d\n" , msix->max_entries, msix->masked, msix->enabled); - rc =vpci_msix_arch_print (msix); + rc =vpci_msix_arch_print (d, msix);if ( rc ) {/*
@@ -318,12 +326,13 @@ void vpci_dump_msi(void)* holding the lock. */ printk ("unable to print all MSI-X entries: %d\n" , rc); -process_pending_softirqs (); -continue ; + goto pdev_done; } }spin_unlock (&pdev->vpci->lock); + pdev_done: +pcidevs_read_unlock ();process_pending_softirqs (); } }
xen/drivers/vpci/msix.c +48 -7
136 lines not on this page · raw message
xen/drivers/vpci/vpci.c +19 -3
@@ -37,7 +37,9 @@ extern vpci_register_init_t *const __end_vpci_array[];void vpci_remove_device (struct pci_dev *pdev) { -if ( !has_vpci (pdev->domain) ) +ASSERT (pcidevs_write_locked ()); + +if ( !has_vpci (pdev->domain) || !pdev->vpci )return ;spin_lock (&pdev->vpci->lock);
@@ -62,6 +64,8 @@ int vpci_add_handlers(struct pci_dev *pdev)unsigned int i;int rc =0 ; +ASSERT (pcidevs_write_locked ()); +if ( !has_vpci (pdev->domain) )return 0 ;
@@ -136,6 +140,8 @@ int vpci_add_register(struct vpci *vpci, vpci_read_t *read_handler,struct list_head *prev;struct vpci_register *r; +ASSERT (pcidevs_write_locked ()); +/* Some sanity checks. */ if ( (size != 1 && size != 2 && size != 4) || offset >= PCI_CFG_SPACE_EXP_SIZE || (offset & (size - 1)) ||
@@ -183,6 +189,8 @@ int vpci_remove_register(struct vpci *vpci, unsigned int offset,const struct vpci_register r = { .offset = offset, .size = size };struct vpci_register *rm; +ASSERT (pcidevs_write_locked ()); +spin_lock (&vpci->lock); list_for_each_entry ( rm, &vpci->handlers, node ) {
@@ -330,10 +338,14 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size)return data; } +pcidevs_read_lock ();/* Find the PCI dev matching the address. */ pdev =pci_get_pdev_by_domain (d, sbdf.seg, sbdf.bus, sbdf.devfn); -if ( !pdev ) +if ( !pdev || (pdev && !pdev->vpci) ) + { +pcidevs_read_unlock ();return vpci_read_hw (sbdf, reg, size); + }spin_lock (&pdev->vpci->lock);
@@ -379,6 +391,7 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size)ASSERT (data_offset < size); }spin_unlock (&pdev->vpci->lock); +pcidevs_read_unlock ();if ( data_offset < size ) {
@@ -441,9 +454,11 @@ void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,* Find the PCI dev matching the address. * Passthrough everything that's not trapped. */ +pcidevs_read_lock (); pdev =pci_get_pdev_by_domain (d, sbdf.seg, sbdf.bus, sbdf.devfn); -if ( !pdev ) +if ( !pdev || (pdev && !pdev->vpci) ) { +pcidevs_read_unlock ();vpci_write_hw (sbdf, reg, size, data);return ; }
@@ -484,6 +499,7 @@ void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,ASSERT (data_offset < size); }spin_unlock (&pdev->vpci->lock); +pcidevs_read_unlock ();if ( data_offset < size )/* Tailing gap, write the remaining. */
xen/include/xen/pci.h +5 -0
@@ -152,6 +152,11 @@ void pcidevs_lock(void);void pcidevs_unlock (void );bool_t __must_check pcidevs_locked (void ); +void pcidevs_read_lock (void ); +int pcidevs_read_trylock (void ); +void pcidevs_read_unlock (void ); +bool __must_check pcidevs_read_locked (void ); +void pcidevs_write_lock (void );void pcidevs_write_unlock (void );bool __must_check pcidevs_write_locked (void );
xen/include/xen/vpci.h +1 -1
@@ -173,7 +173,7 @@ int __must_check vpci_msix_arch_enable_entry(struct vpci_msix_entry *entry,int __must_check vpci_msix_arch_disable_entry (struct vpci_msix_entry *entry,const struct pci_dev *pdev);void vpci_msix_arch_init_entry (struct vpci_msix_entry *entry); -int vpci_msix_arch_print (const struct vpci_msix *msix); +int vpci_msix_arch_print (const struct domain *d,const struct vpci_msix *msix);/* * Helper functions to fetch MSIX related data. They are used by both the
Signature · 1 line
2.25.1