Skip to contentSkip to conversation

x86: yet more PCI locking adjustments

PATCH In review Jan Beulich 2 participants 13 messages Oct 29 – Nov 17

lore
Share & tools
lore
#0 lore raw

cover x86: yet more PCI locking adjustments

Continuing from 13efde94f4af ("x86/vPCI: tighten locking assertions").

1: physdev: use less heavy locking in physdev_unmap_pirq() 2: IRQ: use less heavy locking in free_domain_pirqs() 3: MSI: drop redundant checking
4: tighten PCI-related locking assertions

Jan

#1 lore raw

PATCH 1/4 x86/physdev: use less heavy locking in physdev_unmap_pirq()

Like its mapping counterpart, in the GSI case no (PCI-related) lock would be needed at all, while in the MSI cases the domain's PCI lock is sufficient to hold.

xen/arch/x86/physdev.c +2 -2
@@ -156,11 +156,11 @@ int physdev_unmap_pirq(struct domain *d,
             return ret;
     }
 
-    pcidevs_lock();
+    read_lock(&d->pci_lock);
     write_lock(&d->event_lock);
     ret = unmap_domain_pirq(d, pirq);
     write_unlock(&d->event_lock);
-    pcidevs_unlock();
+    read_unlock(&d->pci_lock);
 
     return ret;
 }
#2 lore raw

Re: [PATCH 1/4] x86/physdev: use less heavy locking in physdev_unmap_pirq()

Quoted 9 lines · Jan Beulich

Like its mapping counterpart, in the GSI case no (PCI-related) lock would be needed at all, while in the MSI cases the domain's PCI lock is sufficient to hold.

xen/arch/x86/physdev.c int physdev_unmap_pirq(struct domain *d, · line 163

Quoted 10 lines
xen/arch/x86/physdev.c +2 -2
@@ -156,11 +156,11 @@ int physdev_unmap_pirq(struct domain *d,
             return ret;
     }
 
-    pcidevs_lock();
+    read_lock(&d->pci_lock);
     write_lock(&d->event_lock);
     ret = unmap_domain_pirq(d, pirq);
     write_unlock(&d->event_lock);
-    pcidevs_unlock();
+    read_unlock(&d->pci_lock);

I think there's possibly one pre-patch that would be needed before making this change. Callers of pci_restore_msi_state() only hold the pcidevs lock, dropping pcidevs lock here we would be allowing changes to the list while there's possibly a concurrent access by pci_restore_msi_state().

I think pci_restore_msi_state() (or its callers) need to hold the d->event_lock, and this likely wants to be a pre-patch. Not holding d->event_lock while iterating over pdev->msi_list in pci_restore_msi_state() seems like a pre-existing issue that would be uncovered by this patch, albeit it's not clear what d->event_lock is supposed to protect exactly.

Thanks, Roger.

#3 lore raw

PATCH 2/4 x86/IRQ: use less heavy locking in free_domain_pirqs()

In reply to Jan Beulich

Holding the domain's PCI lock (alongside its event lock) is sufficient around unmap_domain_pirq().

xen/arch/x86/irq.c +2 -2
@@ -2516,7 +2516,7 @@ void free_domain_pirqs(struct domain *d)
 {
     int i;
 
-    pcidevs_lock();
+    read_lock(&d->pci_lock);
     write_lock(&d->event_lock);
 
     for ( i = 0; i < d->nr_pirqs; i++ )
@@ -2524,7 +2524,7 @@ void free_domain_pirqs(struct domain *d)
             unmap_domain_pirq(d, i);
 
     write_unlock(&d->event_lock);
-    pcidevs_unlock();
+    read_unlock(&d->pci_lock);
 }
 
 static void cf_check dump_irqs(unsigned char key)
#5 lore raw

Re: [PATCH 2/4] x86/IRQ: use less heavy locking in free_domain_pirqs()

Quoted 7 lines · Roger Pau Monné
Quoted 4 lines · Jan Beulich

Holding the domain's PCI lock (alongside its event lock) is sufficient around unmap_domain_pirq().

Thanks, but: Considering your reply to patch 1, did you forget to make this conditional upon the same prereq change that you outline there?

Jan

#6 lore raw

Re: [PATCH 2/4] x86/IRQ: use less heavy locking in free_domain_pirqs()

Quoted 11 lines · Jan Beulich
Quoted 7 lines · Roger Pau Monné
Quoted 4 lines · Jan Beulich

Holding the domain's PCI lock (alongside its event lock) is sufficient around unmap_domain_pirq().

Thanks, but: Considering your reply to patch 1, did you forget to make this conditional upon the same prereq change that you outline there?

Strictly speaking this function is only called from arch_domain_destroy(), which is done after PCI devices are deassigned and MSIs are torn down. In this context unmap_domain_pirq() is only caled against GSIs. But yes, for correctness (and to avoid confusion) I should have tied it to the same request as patch 1.

Thanks, Roger.

#7 lore raw

PATCH 3/4 x86/MSI: drop redundant checking

In reply to Jan Beulich

__pci_enable_msi{,x}() are local helpers of pci_enable_msi(). That one already checks for pdev to be non-NULL, and it also asserts the necessary locking state. msi_capability_init() in turn is a local helper of __pci_enable_msi().

xen/arch/x86/msi.c +0 -11
@@ -600,7 +600,6 @@ static int msi_capability_init(struct pc
     unsigned int i, mpos;
     uint16_t control;
 
-    ASSERT_PDEV_LIST_IS_READ_LOCKED(dev->domain);
     pos = dev->msi_pos;
     if ( !pos )
         return -ENODEV;
@@ -1004,11 +1003,6 @@ static int __pci_enable_msi(struct pci_d
 {
     struct msi_desc *old_desc;
 
-    if ( !pdev )
-        return -ENODEV;
-
-    ASSERT_PDEV_LIST_IS_READ_LOCKED(pdev->domain);
-
     old_desc = find_msi_entry(pdev, msi->irq, PCI_CAP_ID_MSI);
     if ( old_desc )
     {
@@ -1059,11 +1053,6 @@ static int __pci_enable_msix(struct pci_
 {
     struct msi_desc *old_desc;
 
-    if ( !pdev )
-        return -ENODEV;
-
-    ASSERT_PDEV_LIST_IS_READ_LOCKED(pdev->domain);
-
     if ( !pdev->msix )
         return -ENODEV;
 
Acked-by on v1 3/4
#8 lore raw

Re: [PATCH 3/4] x86/MSI: drop redundant checking

Quoted 6 lines · Jan Beulich

__pci_enable_msi{,x}() are local helpers of pci_enable_msi(). That one already checks for pdev to be non-NULL, and it also asserts the necessary locking state. msi_capability_init() in turn is a local helper of __pci_enable_msi().

Thanks, Roger.

#9 lore raw

PATCH 4/4 x86: tighten PCI-related locking assertions

In reply to Jan Beulich

With recent changes the .pi_update_irte() hook as well as {,un}map_domain_pirq() and allocate_and_map_msi_pirq() won't be called anymore with the global PCI lock held. Convert the assertions to check for just the domain's PCI lock.

With that the somewhat ugly ASSERT_PDEV_LIST_IS_READ_LOCKED() isn't used outside of msi.c anymore, so move the #define there.

---
It being just two use sites which remain, we may want to do away with ASSERT_PDEV_LIST_IS_READ_LOCKED() altogether. Thoughts? (It also isn't qite clear to me why its #define(s) sit inside an #ifdef: The expression used ought to be fine to use also in release builds, as it looks.)

xen/arch/x86/hvm/vmx/vmx.c +1 -1
@@ -419,7 +419,7 @@ static int cf_check vmx_pi_update_irte(c
 
     spin_unlock_irq(&desc->lock);
 
-    ASSERT_PDEV_LIST_IS_READ_LOCKED(msi_desc->dev->domain);
+    ASSERT(rw_is_locked(&msi_desc->dev->domain->pci_lock));
 
     rc = iommu_update_ire_from_msi(msi_desc, &msg);
     if ( rc > 0 )
xen/arch/x86/irq.c +3 -3
@@ -2250,7 +2250,7 @@ int map_domain_pirq(
         struct pci_dev *pdev;
         unsigned int nr = 0;
 
-        ASSERT_PDEV_LIST_IS_READ_LOCKED(d);
+        ASSERT(rw_is_locked(&d->pci_lock));
 
         ret = -ENODEV;
         if ( !cpu_has_apic )
@@ -2410,7 +2410,7 @@ int unmap_domain_pirq(struct domain *d,
     if ( (pirq < 0) || (pirq >= d->nr_pirqs) )
         return -EINVAL;
 
-    ASSERT_PDEV_LIST_IS_READ_LOCKED(d);
+    ASSERT(rw_is_locked(&d->pci_lock));
     ASSERT(rw_is_write_locked(&d->event_lock));
 
     info = pirq_info(d, pirq);
@@ -3021,7 +3021,7 @@ int allocate_and_map_msi_pirq(struct dom
 {
     int irq = -1, pirq, ret;
 
-    ASSERT_PDEV_LIST_IS_READ_LOCKED(d);
+    ASSERT(rw_is_locked(&d->pci_lock));
 
     switch ( type )
     {
xen/arch/x86/msi.c +13 -0
@@ -737,6 +737,19 @@ static uint64_t read_pci_mem_bar(const s
     return (addr & PCI_BASE_ADDRESS_MEM_MASK) + disp;
 }
 
+#ifndef NDEBUG
+/*
+ * Check to ensure there will be no changes to the entries in d->pdev_list (but
+ * not the contents of each entry).
+ * This check is not suitable for protecting other state or critical regions.
+ */
+#define ASSERT_PDEV_LIST_IS_READ_LOCKED(d)                               \
+        /* NB: d may be evaluated multiple times, or not at all */       \
+        ASSERT(pcidevs_locked() || ((d) && rw_is_locked(&(d)->pci_lock)))
+#else
+#define ASSERT_PDEV_LIST_IS_READ_LOCKED(d) ((void)(d))
+#endif
+
 /**
  * msix_capability_init - configure device's MSI-X capability
  * @dev: pointer to the pci_dev data structure of MSI-X device function
xen/include/xen/pci.h +0 -13
@@ -204,19 +204,6 @@ static always_inline bool pcidevs_tryloc
     return lock_evaluate_nospec(pcidevs_trylock_unsafe());
 }
 
-#ifndef NDEBUG
-/*
- * Check to ensure there will be no changes to the entries in d->pdev_list (but
- * not the contents of each entry).
- * This check is not suitable for protecting other state or critical regions.
- */
-#define ASSERT_PDEV_LIST_IS_READ_LOCKED(d)                               \
-        /* NB: d may be evaluated multiple times, or not at all */       \
-        ASSERT(pcidevs_locked() || ((d) && rw_is_locked(&(d)->pci_lock)))
-#else
-#define ASSERT_PDEV_LIST_IS_READ_LOCKED(d) ((void)(d))
-#endif
-
 bool pci_known_segment(u16 seg);
 bool pci_device_detect(pci_sbdf_t sbdf);
 int scan_pci_devices(void);
Acked-by on v1 4/4
#10 lore raw

Re: [PATCH 4/4] x86: tighten PCI-related locking assertions

Quoted 9 lines · Jan Beulich

With recent changes the .pi_update_irte() hook as well as {,un}map_domain_pirq() and allocate_and_map_msi_pirq() won't be called anymore with the global PCI lock held. Convert the assertions to check for just the domain's PCI lock.

With that the somewhat ugly ASSERT_PDEV_LIST_IS_READ_LOCKED() isn't used outside of msi.c anymore, so move the #define there.

I agree about keeping a single ASSERT_PDEV_LIST_IS_READ_LOCKED() definitions and dropping the unneeded NDEBUG preprocessor conditional.

Quoted 3 lines

---
It being just two use sites which remain, we may want to do away with ASSERT_PDEV_LIST_IS_READ_LOCKED() altogether. Thoughts?

IIRC the reason for having this expression was so it could be used everywhere to ensure the domain pdev list was somehow locked (either by pcidevs lock or the per-domain rwlock).

If we now have fine grained control over the locking it possibly makes no sense to keep it.

Quoted 3 lines

(It also isn't
qite clear to me why its #define(s) sit inside an #ifdef: The expression used ought to be fine to use also in release builds, as it looks.)

Hm, I'm not sure. It looks like the debug expression could be used in non-debug builds.

Quoted 48 lines
xen/arch/x86/hvm/vmx/vmx.c +1 -1
@@ -419,7 +419,7 @@ static int cf_check vmx_pi_update_irte(c
 
     spin_unlock_irq(&desc->lock);
 
-    ASSERT_PDEV_LIST_IS_READ_LOCKED(msi_desc->dev->domain);
+    ASSERT(rw_is_locked(&msi_desc->dev->domain->pci_lock));
 
     rc = iommu_update_ire_from_msi(msi_desc, &msg);
     if ( rc > 0 )
xen/arch/x86/irq.c +3 -3
@@ -2250,7 +2250,7 @@ int map_domain_pirq(
         struct pci_dev *pdev;
         unsigned int nr = 0;
 
-        ASSERT_PDEV_LIST_IS_READ_LOCKED(d);
+        ASSERT(rw_is_locked(&d->pci_lock));
 
         ret = -ENODEV;
         if ( !cpu_has_apic )
@@ -2410,7 +2410,7 @@ int unmap_domain_pirq(struct domain *d,
     if ( (pirq < 0) || (pirq >= d->nr_pirqs) )
         return -EINVAL;
 
-    ASSERT_PDEV_LIST_IS_READ_LOCKED(d);
+    ASSERT(rw_is_locked(&d->pci_lock));
     ASSERT(rw_is_write_locked(&d->event_lock));
 
     info = pirq_info(d, pirq);
@@ -3021,7 +3021,7 @@ int allocate_and_map_msi_pirq(struct dom
 {
     int irq = -1, pirq, ret;
 
-    ASSERT_PDEV_LIST_IS_READ_LOCKED(d);
+    ASSERT(rw_is_locked(&d->pci_lock));
 
     switch ( type )
     {
xen/arch/x86/msi.c +2 -0
@@ -737,6 +737,19 @@ static uint64_t read_pci_mem_bar(const s
     return (addr & PCI_BASE_ADDRESS_MEM_MASK) + disp;
 }
 
+#ifndef NDEBUG
+/*

xen/arch/x86/msi.c static uint64_t read_pci_mem_bar(const s · line 751

Quoted 10 lines
xen/arch/x86/msi.c +10 -0
+ * Check to ensure there will be no changes to the entries in d->pdev_list (but
+ * not the contents of each entry).
+ * This check is not suitable for protecting other state or critical regions.
+ */
+#define ASSERT_PDEV_LIST_IS_READ_LOCKED(d)                               \
+        /* NB: d may be evaluated multiple times, or not at all */       \
+        ASSERT(pcidevs_locked() || ((d) && rw_is_locked(&(d)->pci_lock)))
+#else
+#define ASSERT_PDEV_LIST_IS_READ_LOCKED(d) ((void)(d))
+#endif

Maybe while moving it you can leave just the !NDEBUG variant?Q

Thanks, Roger.

#11 lore raw

Re: [PATCH 4/4] x86: tighten PCI-related locking assertions

Quoted 6 lines · Roger Pau Monné
Quoted 5 lines · Jan Beulich

With recent changes the .pi_update_irte() hook as well as {,un}map_domain_pirq() and allocate_and_map_msi_pirq() won't be called anymore with the global PCI lock held. Convert the assertions to check for just the domain's PCI lock.

Quoted 6 lines
Quoted 4 lines

With that the somewhat ugly ASSERT_PDEV_LIST_IS_READ_LOCKED() isn't used outside of msi.c anymore, so move the #define there.

Thanks. I'm a bit puzzled about your further remarks, though.

Quoted 2 lines

I agree about keeping a single ASSERT_PDEV_LIST_IS_READ_LOCKED() definitions and dropping the unneeded NDEBUG preprocessor conditional.

Here you look to agree with ...

Quoted 4 lines
Quoted 3 lines

---
It being just two use sites which remain, we may want to do away with ASSERT_PDEV_LIST_IS_READ_LOCKED() altogether. Thoughts?

Quoted 10 lines

IIRC the reason for having this expression was so it could be used everywhere to ensure the domain pdev list was somehow locked (either by pcidevs lock or the per-domain rwlock).

If we now have fine grained control over the locking it possibly makes no sense to keep it.

Quoted 3 lines

(It also isn't
qite clear to me why its #define(s) sit inside an #ifdef: The expression used ought to be fine to use also in release builds, as it looks.)

... this observation, just to ...

Quoted 2 lines

Hm, I'm not sure. It looks like the debug expression could be used in non-debug builds.

... voice uncertainty here. And then ...

Quoted 6 lines
Quoted 6 lines
xen/arch/x86/msi.c +0 -0
@@ -737,6 +737,19 @@ static uint64_t read_pci_mem_bar(const s
     return (addr & PCI_BASE_ADDRESS_MEM_MASK) + disp;
 }
 
Quoted 14 lines

xen/arch/x86/msi.c static uint64_t read_pci_mem_bar(const s · line 751

Quoted 12 lines
xen/arch/x86/msi.c +12 -0
+#ifndef NDEBUG
+/*
+ * Check to ensure there will be no changes to the entries in d->pdev_list (but
+ * not the contents of each entry).
+ * This check is not suitable for protecting other state or critical regions.
+ */
+#define ASSERT_PDEV_LIST_IS_READ_LOCKED(d)                               \
+        /* NB: d may be evaluated multiple times, or not at all */       \
+        ASSERT(pcidevs_locked() || ((d) && rw_is_locked(&(d)->pci_lock)))
+#else
+#define ASSERT_PDEV_LIST_IS_READ_LOCKED(d) ((void)(d))
+#endif

Maybe while moving it you can leave just the !NDEBUG variant?Q

... you again suggest doing what my remark was effectively suggesting as well.

Jan

#12 lore raw

Re: [PATCH 4/4] x86: tighten PCI-related locking assertions

Quoted 36 lines · Jan Beulich
Quoted 6 lines · Roger Pau Monné
Quoted 5 lines · Jan Beulich

With recent changes the .pi_update_irte() hook as well as {,un}map_domain_pirq() and allocate_and_map_msi_pirq() won't be called anymore with the global PCI lock held. Convert the assertions to check for just the domain's PCI lock.

Quoted 6 lines
Quoted 4 lines

With that the somewhat ugly ASSERT_PDEV_LIST_IS_READ_LOCKED() isn't used outside of msi.c anymore, so move the #define there.

Thanks. I'm a bit puzzled about your further remarks, though.

Quoted 2 lines

I agree about keeping a single ASSERT_PDEV_LIST_IS_READ_LOCKED() definitions and dropping the unneeded NDEBUG preprocessor conditional.

Here you look to agree with ...

Quoted 4 lines
Quoted 3 lines

---
It being just two use sites which remain, we may want to do away with ASSERT_PDEV_LIST_IS_READ_LOCKED() altogether. Thoughts?

Quoted 10 lines

IIRC the reason for having this expression was so it could be used everywhere to ensure the domain pdev list was somehow locked (either by pcidevs lock or the per-domain rwlock).

If we now have fine grained control over the locking it possibly makes no sense to keep it.

Quoted 3 lines

(It also isn't
qite clear to me why its #define(s) sit inside an #ifdef: The expression used ought to be fine to use also in release builds, as it looks.)

Quoted 6 lines

... this observation, just to ...

Quoted 2 lines

Hm, I'm not sure. It looks like the debug expression could be used in non-debug builds.

... voice uncertainty here. And then ...

Uncertainty about why debug vs non-debug variants of the expression were added in the first place :). I agree with you and see no need for them.

Thanks, Roger