Skip to contentSkip to conversation

Make the pcidevs_lock a recursive one

PATCH Superseded Quan Xu 5 participants 20 messages Mar 9, 2016 – Mar 11, 2016

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Quan Xu

author
#0 lore raw

v3 cover Make the pcidevs_lock a recursive one

This patch set makes the pcidevs_lock a recursive one. It is a prereq patch set for Patch:'VT-d Device-TLB flush issue', as the pcidevs_lock may be recursively held for hiding the ATS device, when VT-d Device-TLB flush timed out.

In detail:
1. Fix a bug found in AMD IOMMU initialization.
pcidevs_lock should be held with interrupt enabled. However there remains an exception in AMD IOMMU code, where the lock is acquired with interrupt disabled. This inconsistency might lead to deadlock.

The fix is straightforward to use spin_lock instead. Also interrupt has been enabled when this function is invoked, so we're sure consistency around pcidevs_lock can be guaranteed after this fix.

2. Make the pcidevs_lock a recursive one.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Andrew Cooper <[email protected]>
CC Suravee Suthikulpanit <[email protected]>
CC Feng Wu <[email protected]>
CC Kevin Tian <[email protected]>
CC Dario Faggioli <[email protected]>

--Changes in v3:
* Enhance patch1/2 changelog.

Quan Xu (2):
IOMMU/spinlock: Fix a bug found in AMD IOMMU initialization IOMMU/spinlock: Make the pcidevs_lock a recursive one

 xen/arch/x86/domctl.c                       |  8 +--
 xen/arch/x86/hvm/vmsi.c                     |  4 +-
 xen/arch/x86/irq.c                          |  8 +--
 xen/arch/x86/msi.c                          | 16 ++---
 xen/arch/x86/pci.c                          |  4 +-
 xen/arch/x86/physdev.c                      | 16 ++---
 xen/common/sysctl.c                         |  4 +-
 xen/drivers/passthrough/amd/iommu_init.c    |  9 ++-
 xen/drivers/passthrough/amd/iommu_map.c     |  2 +-
 xen/drivers/passthrough/amd/pci_amd_iommu.c |  4 +-
 xen/drivers/passthrough/pci.c               | 96 +++++++++++++++++------------
 xen/drivers/passthrough/vtd/intremap.c      |  2 +-
 xen/drivers/passthrough/vtd/iommu.c         | 14 ++---
 xen/drivers/video/vga.c                     |  4 +-
 xen/include/xen/pci.h                       |  5 +-
 15 files changed, 109 insertions(+), 87 deletions(-)
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
author
#1 lore raw

PATCH v3 1/2 IOMMU/spinlock: Fix a bug found in AMD IOMMU initialization

pcidevs_lock should be held with interrupt enabled. However there remains an exception in AMD IOMMU code, where the lock is acquired with interrupt disabled. This inconsistency might lead to deadlock.

The fix is straightforward to use spin_lock instead. Also interrupt has been enabled when this function is invoked, so we're sure consistency around pcidevs_lock can be guaranteed after this fix.

CC Suravee Suthikulpanit <[email protected]>
CC Dario Faggioli <[email protected]>
CC Jan Beulich <[email protected]>
---
 xen/drivers/passthrough/amd/iommu_init.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
xen/drivers/passthrough/amd/iommu_init.c +2 -3
index d90a2d2..a400497 100644
@@ -778,7 +778,6 @@ static bool_t __init set_iommu_interrupt_handler(struct amd_iommu *iommu)
 {
     int irq, ret;
     hw_irq_controller *handler;
-    unsigned long flags;
     u16 control;
 
     irq = create_irq(NUMA_NO_NODE);
@@ -788,10 +787,10 @@ static bool_t __init set_iommu_interrupt_handler(struct amd_iommu *iommu)
         return 0;
     }
 
-    spin_lock_irqsave(&pcidevs_lock, flags);
+    spin_lock(&pcidevs_lock);
     iommu->msi.dev = pci_get_pdev(iommu->seg, PCI_BUS(iommu->bdf),
                                   PCI_DEVFN2(iommu->bdf));
-    spin_unlock_irqrestore(&pcidevs_lock, flags);
+    spin_unlock(&pcidevs_lock);
     if ( !iommu->msi.dev )
     {
         AMD_IOMMU_DEBUG("IOMMU: no pdev for %04x:%02x:%02x.%u\n",
Signature · 7 lines
1.9.1


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

Re: [PATCH v3 1/2] IOMMU/spinlock: Fix a bug found in AMD IOMMU initialization

Quoted 2 lines · Quan Xu

pcidevs_lock should be held with interrupt enabled.

There's a message from Jan when he says:
<<Well, I'd say something like "pcidevs_lock doesn't require interrupts to be disabled while being acquired".>>

:-O

Quoted 5 lines

However there remains
an exception in AMD IOMMU code, where the lock is acquired with interrupt
disabled. This inconsistency might lead to deadlock.

I appreciate that 'might' is weaker than 'can'. Personally, I still dob't find this correct, or at least clear enough, referred to this patch, but I won't be in the way because of this.

Quoted 6 lines

The fix is straightforward to use spin_lock instead. Also interrupt has been
enabled when this function is invoked, so we're sure consistency around
pcidevs_lock can be guaranteed after this fix.

And I also can't really get what "so we're sure consistency around pcidevs_lock can be guaranteed after this fix" actually means, but again, that's probably me, and it's fine.

However,

Quoted 3 lines

This still stands **only** if the very first sentence "pcidevs_lock should be held with interrupt enabled" is changed to "pcidevs_lock doesn't require interrupts to be disabled while being acquired".

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

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

author
#3 lore raw

Re: [PATCH v3 1/2] IOMMU/spinlock: Fix a bug found in AMD IOMMU initialization

CC Kevin,

Quoted 25 lines · On March 09, 2016 11:00pm, <[email protected]> wrote:
Quoted 2 lines · Quan Xu

pcidevs_lock should be held with interrupt enabled.

There's a message from Jan when he says:
<<Well, I'd say something like "pcidevs_lock doesn't require interrupts to be disabled while being acquired".>>

:-O

Quoted 4 lines

However there remains
an exception in AMD IOMMU code, where the lock is acquired with interrupt disabled. This inconsistency might lead to deadlock.

I appreciate that 'might' is weaker than 'can'. Personally, I still dob't find this correct, or at least clear enough, referred to this patch, but I won't be in the way because of this.

Quoted 4 lines

The fix is straightforward to use spin_lock instead. Also interrupt has been enabled when this function is invoked, so we're sure consistency around pcidevs_lock can be guaranteed after this fix.

And I also can't really get what "so we're sure consistency around pcidevs_lock can be guaranteed after this fix" actually means, but again, that's probably me, and it's fine.

Quoted 9 lines

However,

Quoted 3 lines

This still stands **only** if the very first sentence "pcidevs_lock should be held with interrupt enabled" is changed to "pcidevs_lock doesn't require interrupts to be disabled while being acquired".

Dario,
I am open for this change:).

When I read:
1. (look at "Lesson 3: spinlocks revisited.")
https://www.kernel.org/doc/Documentation/locking/spinlocks.txt
2. comment inside check_lock(), in xen/common/spinlock.c, in Xen's codebase.
3. the "Linux Device Drivers, 3rd Edition" book , http://www.makelinux.net/ldd3/chp-5-sect-5

I found that:
- "We partition locks into IRQ-safe (__always__ held with IRQs disabled) and IRQ-unsafe (__always__ held with IRQs enabled) types".

It looks like Kevin's description is better.

I also found that you mentioned in this thread:
"... __except for very special situations__". If it is true, I think your description is better.

Thanks for your time..:):)

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

#4 lore raw

Re: [PATCH v3 1/2] IOMMU/spinlock: Fix a bug found in AMD IOMMU initialization

In reply to Quan Xu

Hi Quan,

Can I ask a dumb question?

Quoted 3 lines · Quan Xu

pcidevs_lock should be held with interrupt enabled. However there remains an exception in AMD IOMMU code, where the lock is acquired with interrupt disabled. This inconsistency might lead to deadlock.

Why will this inconsistency lead to deadlock?
I understand the difference between spin_lock_irqsave(), which disable interrupt, and spin_lock(), which allows interrupt, however, I didn't get why misuse the spin_lock_irqsave() at the place of spin_lock() could potentially lead to a deadlock?
Would you minding pointing me to somewhere I can find the reason or enlighten me?

Thank you very much!

Best,

Meng

-----------
Meng Xu
PhD Student in Computer and Information Science
University of Pennsylvania
http://www.cis.upenn.edu/~mengxu/

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

author
#5 lore raw

Re: [PATCH v3 1/2] IOMMU/spinlock: Fix a bug found in AMD IOMMU initialization

Quoted 10 lines · On March 11, 2016 11:25am, <[email protected]> wrote:
Quoted 3 lines · Quan Xu

pcidevs_lock should be held with interrupt enabled. However there remains an exception in AMD IOMMU code, where the lock is acquired with interrupt disabled. This inconsistency might lead to deadlock.

Why will this inconsistency lead to deadlock?
I understand the difference between spin_lock_irqsave(), which disable interrupt, and spin_lock(), which allows interrupt, however, I didn't get why misuse the spin_lock_irqsave() at the place of spin_lock() could potentially lead to a deadlock?

For above 2 questions:
Mix is just one of _possible_ scenarios(IMO, not 100% leading to a deadlock):

-where an interrupt tries to lock an already locked variable. This is ok if the other interrupt happens on another CPU, but it is _not_ ok if the interrupt happens on the same CPU that already holds the lock

I got it from the bellow 2 points:
1).As Jan mentioned, The implication from disabling interrupts while acquiring a lock is that the lock is also being acquired by some interrupt handler.
If you mix acquire types, the one not disabling interrupts is prone to be interrupted, and the interrupt trying to get hold of the lock the same CPU already owns.

2). Comment inside check_lock(),
we partition locks into IRQ-safe (always held with IRQs disabled) and IRQ-unsafe (always held with IRQs enabled) types. The convention for every lock must be consistently observed else we can deadlock in IRQ-context rendezvous functions (__a rendezvous which gets every CPU into IRQ context before any CPU is released from the rendezvous__). If we can mix IRQ-disabled and IRQ-enabled callers, the following can happen:
* Lock is held by CPU A, with IRQs enabled
* CPU B is spinning on same lock, with IRQs disabled
* Rendezvous starts -- CPU A takes interrupt and enters rendezbous spin
* DEADLOCK -- CPU B will never enter rendezvous, CPU A will never exit the rendezvous, and will hence never release the lock.

To guard against this subtle bug we latch the IRQ safety of every spinlock in the system, on first use.

Quoted 3 lines

Would you minding pointing me to somewhere I can find the reason or enlighten me?

Some reference material:

* (look at "Lesson 3: spinlocks revisited.")
https://www.kernel.org/doc/Documentation/locking/spinlocks.txt
* comment inside check_lock(), in xen/common/spinlock.c, in Xen's codebase.
* the "Linux Device Drivers, 3rd Edition" book , http://www.makelinux.net/ldd3/chp-5-sect-5

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

#6 lore raw

Re: [PATCH v3 1/2] IOMMU/spinlock: Fix a bug found in AMD IOMMU initialization

Quoted 17 lines · Quan
Quoted 8 lines · On March 11, 2016 11:25am, <[email protected]> wrote:
Quoted 6 lines · Quan Xu

pcidevs_lock should be held with interrupt enabled. However there remains an exception in AMD IOMMU code, where the lock is acquired
with interrupt disabled. This inconsistency might lead to deadlock.

Quoted 8 lines

Why will this inconsistency lead to deadlock?
I understand the difference between spin_lock_irqsave(), which disable interrupt,
and spin_lock(), which allows interrupt, however, I didn't get why misuse the
spin_lock_irqsave() at the place of spin_lock() could potentially lead to a
deadlock?

Quoted 8 lines


 1).As Jan mentioned, The implication from disabling interrupts while acquiring a lock is that the lock is also being acquired by some interrupt handler.
  If you mix acquire types, the one not disabling interrupts is prone to be interrupted, and the interrupt trying to get hold of the lock the same CPU already owns.

The key issue is whether or not a lock can be acquired from IRQ context (i.e., in an interrupt handler, or a function called by that, as a result of an interrupt occurring).

For lock that can, IRQ disabling variants must be used *everywhere* the lock is taken (so, e.g., not only when it is actually taken from IRQ context, just *always*!).

If that rule is not honored, we are ok if the lock is taken on CPUA, and the interrupt handled on CPUB:

  CPUA              CPUB
  .                 .
  .                 .
  spin_lock(l)      .
  .                 .
  .                 . <-- Interrupt!
  .                        .
  .                       spin_lock(l); //spins on l .                       x             //spins on l .                       x             //spins on l spin_unlock(l)          .             //takes l .                       .
  .                       spin_unlock(l);
  .                 . <-- .
  .                 .

but the following can happen, if the interrupt is delivered to the CPU that has already taken the lock:

     CPU
     .
     .
[1]  spin_lock(l);       //takes l
     .
     . <-- Interrupt!
           .
           spin_lock(l) // CPU deadlocks

If, in the latter case, spin_lock_irq(l) were used at [1], things would have been fine, as the interrupt wouldn't have occurred until l weren't released:

  CPU
  .
  .
  spin_lock_irq(l)                        //takes l .
  . <---------------- Interrupt!
  .                   -                   //IRQs are disabled .                   -                   //IRQs are disabled .                   -                   //IRQs are disabled spin_unlock_irq(l)  .                   //IRQs re-hanbled spin_lock_irq(l);   //takes l .
                      .
                      spin_unlock_irq(l);
 . <----------------- .
 .

Note that whether spin_lock_irq() or spin_lock_irqsave() should be used, is completely independent from this, and it must be decided according to whether IRQs are disabled already or not when taking the lock. And (quite obviously, but since wre're here) it is fine to mix spin_lock_irq() and spin_lock_irqsave(), as they both disable interrupts, which is what matters.

So, now, if we know for sure that a lock is _never_ever_ever_ taken from interrupt context, can we mix spin_lock() and spin_lock_irq() on it (for whatever reason)? Well, as far as the above reasoning is concerned, yes.

In fact, the deadlock arises because IRQs interrupt asynchronously what the CPU is doing, and that can happen when the CPU has taken the lock already. But if the 'asynchronous' part goes away, we really don't care whether a lock is take at time t1 with IRQ enabled, and at time t2 with IRQ disabled, don't you think?

Well, here it is where what the comment inside check_lock() describes comes into play. As quoted by Qaun already:

Quoted 16 lines

 2). Comment inside check_lock(),
we partition locks into IRQ-safe (always held with IRQs disabled) and IRQ-unsafe (always held with IRQs enabled) types. The convention for every lock must be consistently observed else we can deadlock in IRQ-context rendezvous functions (__a rendezvous which gets every CPU into IRQ context before any CPU is released from the rendezvous__). If we can mix IRQ-disabled and IRQ-enabled callers, the following can happen:
 * Lock is held by CPU A, with IRQs enabled
 * CPU B is spinning on same lock, with IRQs disabled
 * Rendezvous starts -- CPU A takes interrupt and enters rendezbous spin
 * DEADLOCK -- CPU B will never enter rendezvous, CPU A will never exit
               the rendezvous, and will hence never release the lock.

Quoted 3 lines

To guard against this subtle bug we latch the IRQ safety of every spinlock in the system, on first use.

This is a very good safety measure, but it can be quite a restriction in some (luckily limited) cases. And that's why it is possible -- although absolutely discouraged-- to relax it. See, for an example of this, the comment in start_secondary(), in xen/arch/x86/smpboot.c:

    ...
    /*
     * Just as during early bootstrap, it is convenient here to disable
     * spinlock checking while we have IRQs disabled. This allows us to
     * acquire IRQ-unsafe locks when it would otherwise be disallowed. *
     * It is safe because the race we are usually trying to avoid involves
     * a group of CPUs rendezvousing in an IPI handler, where one cannot
     * join because it is spinning with IRQs disabled waiting to acquire a
     * lock held by another in the rendezvous group (the lock must be an
     * IRQ-unsafe lock since the CPU took the IPI after acquiring it, and
     * hence had IRQs enabled). This is a deadlock scenario.
     *
     * However, no CPU can be involved in rendezvous until it is online,
     * hence no such group can be waiting for this CPU until it is
     * visible in cpu_online_map. Hence such a deadlock is not possible. */

    spin_debug_disable();
    ...

Just FTR, at least as far as I can remember, Linux does not enforce anything like that.

Hope this clarifies things.

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

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

author
#7 lore raw

Re: [PATCH v3 1/2] IOMMU/spinlock: Fix a bug found in AMD IOMMU initialization

Quoted 90 lines · On March 11, 2016 6:36pm, <Dario Faggioli> wrote:
Quoted 15 lines · Quan
Quoted 7 lines · On March 11, 2016 11:25am, <[email protected]> wrote:
Quoted 5 lines · Quan Xu

pcidevs_lock should be held with interrupt enabled. However there remains an exception in AMD IOMMU code, where the lock is acquired with interrupt disabled. This inconsistency might lead to deadlock.

Quoted 6 lines

Why will this inconsistency lead to deadlock?
I understand the difference between spin_lock_irqsave(), which disable interrupt, and spin_lock(), which allows interrupt, however, I didn't get why misuse the
spin_lock_irqsave() at the place of spin_lock() could potentially lead to a deadlock?

Quoted 7 lines

 1).As Jan mentioned, The implication from disabling interrupts while acquiring a lock is that the lock is also being acquired by some interrupt handler.
  If you mix acquire types, the one not disabling interrupts is prone to be interrupted, and the interrupt trying to get hold of the lock the same CPU already owns.

The key issue is whether or not a lock can be acquired from IRQ context (i.e., in an interrupt handler, or a function called by that, as a result of an interrupt occurring).

For lock that can, IRQ disabling variants must be used *everywhere* the lock is taken (so, e.g., not only when it is actually taken from IRQ context, just *always*!).

If that rule is not honored, we are ok if the lock is taken on CPUA, and the interrupt handled on CPUB:

  CPUA              CPUB
  .                 .
  .                 .
  spin_lock(l)      .
  .                 .
  .                 . <-- Interrupt!
  .                        .
  .                       spin_lock(l); //spins on l .                       x             //spins on l .                       x             //spins on l spin_unlock(l)          .             //takes l .                       .
  .                       spin_unlock(l);
  .                 . <-- .
  .                 .

but the following can happen, if the interrupt is delivered to the CPU that has already taken the lock:

     CPU
     .
     .
[1]  spin_lock(l);       //takes l
     .
     . <-- Interrupt!
           .
           spin_lock(l) // CPU deadlocks

If, in the latter case, spin_lock_irq(l) were used at [1], things would have been fine, as the interrupt wouldn't have occurred until l weren't released:

  CPU
  .
  .
  spin_lock_irq(l)                        //takes l .
  . <---------------- Interrupt!
  .                   -                   //IRQs are disabled .                   -                   //IRQs are disabled .                   -                   //IRQs are disabled spin_unlock_irq(l)  .                   //IRQs re-hanbled spin_lock_irq(l);   //takes l .
                      .
                      spin_unlock_irq(l);
 . <----------------- .
 .

Note that whether spin_lock_irq() or spin_lock_irqsave() should be used, is completely independent from this, and it must be decided according to whether IRQs are disabled already or not when taking the lock. And (quite obviously, but since wre're here) it is fine to mix
spin_lock_irq() and spin_lock_irqsave(), as they both disable interrupts, which is what matters.

Quoted 4 lines

So, now, if we know for sure that a lock is _never_ever_ever_ taken from interrupt context, can we mix spin_lock() and spin_lock_irq() on it (for whatever reason)? Well, as far as the above reasoning is concerned, yes.

I appreciate Dario's explanation.
btw, be careful of spin_lock_irq(), which includes 'ASSERT(local_irq_is_enabled()'..

Quoted 5 lines

In fact, the deadlock arises because IRQs interrupt asynchronously what the CPU is doing, and that can happen when the CPU has taken the lock already. But if the 'asynchronous' part goes away, we really don't care whether a lock is take at time t1 with IRQ enabled, and at time t2 with IRQ disabled, don't you think?

Yes.
Consistency may be helpful to avoid some easy-to-avoid lock errors.
Moreover, without my fix, I think it would not lead dead lock, as the pcidevs_lock is not being taken In IRQ context. Right?

For deadlock, I think the key problems are:
- A lock can be acquired from IRQ context
-The interrupt is delivered to the _same_CPU_ that already holds the lock.

Quan

Quoted 44 lines

Well, here it is where what the comment inside check_lock() describes comes into play. As quoted by Qaun already:

Quoted 16 lines

 2). Comment inside check_lock(),
we partition locks into IRQ-safe (always held with IRQs disabled) and IRQ-unsafe (always held with IRQs enabled) types. The convention for every lock must be consistently observed else we can deadlock in IRQ-context rendezvous functions (__a rendezvous which gets every CPU into IRQ context before any CPU is released from the rendezvous__). If we can mix IRQ-disabled and IRQ-enabled callers, the following can happen:
 * Lock is held by CPU A, with IRQs enabled
 * CPU B is spinning on same lock, with IRQs disabled
 * Rendezvous starts -- CPU A takes interrupt and enters rendezbous spin
 * DEADLOCK -- CPU B will never enter rendezvous, CPU A will never exit
               the rendezvous, and will hence never release the lock.

Quoted 3 lines

To guard against this subtle bug we latch the IRQ safety of every spinlock in the system, on first use.

This is a very good safety measure, but it can be quite a restriction in some (luckily limited) cases. And that's why it is possible -- although absolutely discouraged-- to relax it. See, for an example of this, the comment in start_secondary(), in xen/arch/x86/smpboot.c:

    ...
    /*
     * Just as during early bootstrap, it is convenient here to disable
     * spinlock checking while we have IRQs disabled. This allows us to
     * acquire IRQ-unsafe locks when it would otherwise be disallowed. *
     * It is safe because the race we are usually trying to avoid involves
     * a group of CPUs rendezvousing in an IPI handler, where one cannot
     * join because it is spinning with IRQs disabled waiting to acquire a
     * lock held by another in the rendezvous group (the lock must be an
     * IRQ-unsafe lock since the CPU took the IPI after acquiring it, and
     * hence had IRQs enabled). This is a deadlock scenario.
     *
     * However, no CPU can be involved in rendezvous until it is online,
     * hence no such group can be waiting for this CPU until it is
     * visible in cpu_online_map. Hence such a deadlock is not possible. */

Quoted 7 lines
    spin_debug_disable();
    ...

Just FTR, at least as far as I can remember, Linux does not enforce anything like that.

Hope this clarifies things.

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

#8 lore raw

Re: [PATCH v3 1/2] IOMMU/spinlock: Fix a bug found in AMD IOMMU initialization

Quoted 9 lines · Quan
Quoted 8 lines · On March 11, 2016 6:36pm, <Dario Faggioli> wrote:
Quoted 1 line · Quan

So, now, if we know for sure that a lock is _never_ever_ever_ taken from
interrupt context, can we mix spin_lock() and spin_lock_irq() on it (for whatever
reason)? Well, as far as the above reasoning is concerned, yes.

Quoted 4 lines

I appreciate Dario's explanation.
btw, be careful of spin_lock_irq(), which includes 'ASSERT(local_irq_is_enabled()'..

It does. What about it? That is exactly what marks the difference between spin_lock_irq() and spin_lock_irqsave(). In fact, the former should not be used if interrupts are already disabled because then, when calling the corresponding spin_unlock_irq(), they'd be re-enabled while instead they shouldn't.

But as said, from the point of view of preventing deadlock, for locks taken both from inside and outside IRQ context, they're equivalent.

Quoted 17 lines
Quoted 10 lines

In fact, the deadlock arises because IRQs interrupt asynchronously what the
CPU is doing, and that can happen when the CPU has taken the lock already. But
if the 'asynchronous' part goes away, we really don't care whether a lock is take
at time t1 with IRQ enabled, and at time t2 with IRQ disabled, don't you think?

Yes.
Consistency may be helpful to avoid some easy-to-avoid lock errors. Moreover, without my fix, I think it would not lead dead lock, as the pcidevs_lock is not being taken
In IRQ context. Right?

Quoted 5 lines

For deadlock, I think the key problems are:
  - A lock can be acquired from IRQ context
  -The interrupt is delivered to the _same_CPU_ that already holds the lock.

In your case, pcidevs_lock is certainly not being taken from both inside and outside IRQ context. If it where, using spin_lock() --as it happens basically everywhere in the code-- would be wrong, and using spin_lock_irq[save]() --as it happens in the only case you're patching-
- would be what would be right!

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

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

#9 lore raw

Re: [PATCH v3 1/2] IOMMU/spinlock: Fix a bug found in AMD IOMMU initialization

In reply to Xu, Quan
Quoted 11 lines
Quoted 6 lines

In fact, the deadlock arises because IRQs interrupt asynchronously what the CPU is doing, and that can happen when the CPU has taken the lock already. But if the 'asynchronous' part goes away, we really don't care whether a lock is take at time t1 with IRQ enabled, and at time t2 with IRQ disabled, don't you think?

Yes.
Consistency may be helpful to avoid some easy-to-avoid lock errors.
Moreover, without my fix, I think it would not lead dead lock, as the pcidevs_lock is not being taken In IRQ context. Right?

I think without your fix, the deadlock may still happen due to the rendezvous condition.
CPU A | CPU B
| CPU C

Step 1| spin_lock                           |
Step 2|                                           | spin_lock_irq           |
Step 3|                                            | wait for A to unlock |
Step 4|
              | send rendezvous IPI to A and B
Step 5| receive IPI                             | wait for A to unlock |
Step 6| wait for B to handle the IPI    | wait for A to unlock |
Step 7| spin_unlock

Deadlock occurs at Step 6, IMO.

Unless we can prove that rendezvous won't happen while spin_lock_irqsave is taken, we have the deadlock hazard.

Actually, I'm not quite sure when the rendezvous condition may happen in this situation. So probably, in reality, we don't have the rendezvous condition.

Quoted 7 lines

For deadlock, I think the key problems are:
- A lock can be acquired from IRQ context
-The interrupt is delivered to the _same_CPU_ that already holds the lock.

This is one type of deadlock, not the one due to rendezvous condition, I think. :-)

Thanks and Best Regards,

Meng

-----------
Meng Xu
PhD Student in Computer and Information Science
University of Pennsylvania
http://www.cis.upenn.edu/~mengxu/

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

#10 lore raw

Re: [PATCH v3 1/2] IOMMU/spinlock: Fix a bug found in AMD IOMMU initialization

Quoted 22 lines · Meng Xu
Quoted 5 lines

Yes.
Consistency may be helpful to avoid some easy-to-avoid lock errors. Moreover, without my fix, I think it would not lead dead lock, as the pcidevs_lock is not being taken
In IRQ context. Right?

I think without your fix, the deadlock may still happen due to the rendezvous condition.
           CPU A                                |    CPU B | CPU C
Step 1| spin_lock                           |
Step 2|                                           | spin_lock_irq           |
Step 3|                                            | wait for A to unlock |
Step 4|
              | send rendezvous IPI to A and B
Step 5| receive IPI                             | wait for A to unlock |
Step 6| wait for B to handle the IPI    | wait for A to unlock | Step 7| spin_unlock

Quoted 5 lines

Deadlock occurs at Step 6, IMO.

Unless we can prove that rendezvous won't happen while spin_lock_irqsave is taken, we have the deadlock hazard.

Yes. But, in the case of Quan's patch (without it, I mean), have you seen where in the code it is that we use spin_lock_irqsave()?

It's inside a function that is called during Xen boot, whose callchain starts with iommu_setup(), from __start_xen(). Here's a (big, sorry) code snippet of what is around iommu_setup():

    ...
    init_idle_domain();

    this_cpu(stubs.addr) = alloc_stub_page(smp_processor_id(), &this_cpu(stubs).mfn); BUG_ON(!this_cpu(stubs.addr));

    trap_init();
    rcu_init();

    early_time_init();

    arch_init_memory();

    alternative_instructions();

    local_irq_enable();

    pt_pci_init();

    vesa_mtrr_init();

    acpi_mmcfg_init();

    early_msi_init();

    iommu_setup();    /* setup iommu if available */

    smp_prepare_cpus(max_cpus);

    spin_debug_enable();

    /*
     * Initialise higher-level timer functions. We do this fairly late
     * (after interrupts got enabled) because the time bases and scale
     * factors need to be updated regularly.
     */
    init_xen_time();
    initialize_keytable();
    console_init_postirq();

    system_state = SYS_STATE_smp_boot; do_presmp_initcalls();
    for_each_present_cpu ( i )
    {
        /* Set up cpu_to_node[]. */ srat_detect_node(i);
        /* Set up node_to_cpumask based on cpu_to_node[]. */ numa_add_cpu(i);

        if ( (num_online_cpus() < max_cpus) && !cpu_online(i) ) {
            int ret = cpu_up(i);
            if ( ret != 0 )
                printk("Failed to bring up CPU %u (error %d)\n", i, ret); }
    }

    printk("Brought up %ld CPUs\n", (long)num_online_cpus());
    ...

As you can see, it is only *after* iommu_setup() that we call functions like smp_prepare_cpus(), do_presmp_initcalls(), and then the loop that waits for all the present CPUs to come online.

What that means is that, at iommu_setup() time, there still is only one CPU online, and there is not much chances that one single CPU deadlocks in a rendezvous!

Honestly, the biggest issue that I think Quan's patch solves, is that if we ever want/manage to move spin_debug_enable() up above it, then the BUG_ON in check_lock() would trigger the first time that pcidevs_lock would be taken with interrupts enabled.

Until then, code is technically fine, and, as a matter of fact, I think that removing the locking from that particular instance would be an equally effective fix!

All that being said, consistency is indeed important, and for the sake of it and for other reasons too, even if, strictly speaking, there isn't any actual buggy behavior to be fixed here, and it is worthwhile conforming to a locking pattern that is consistent with the rules that we sat ourselves, unless there's specific reasons not to.

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

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

#11 lore raw

Re: [PATCH v3 1/2] IOMMU/spinlock: Fix a bug found in AMD IOMMU initialization

Quoted 99 lines · Dario Faggioli
Quoted 22 lines · Meng Xu
Quoted 5 lines

Yes.
Consistency may be helpful to avoid some easy-to-avoid lock errors. Moreover, without my fix, I think it would not lead dead lock, as the pcidevs_lock is not being taken
In IRQ context. Right?

I think without your fix, the deadlock may still happen due to the rendezvous condition.
CPU A | CPU B
| CPU C
Step 1| spin_lock |
Step 2| |
spin_lock_irq |
Step 3| | wait for A to
unlock |
Step 4|
| send rendezvous IPI to A and B
Step 5| receive IPI | wait for A to
unlock |
Step 6| wait for B to handle the IPI | wait for A to unlock |
Step 7| spin_unlock

Quoted 5 lines

Deadlock occurs at Step 6, IMO.

Unless we can prove that rendezvous won't happen while spin_lock_irqsave is taken, we have the deadlock hazard.

Yes. But, in the case of Quan's patch (without it, I mean), have you seen where in the code it is that we use spin_lock_irqsave()?

It's inside a function that is called during Xen boot, whose callchain starts with iommu_setup(), from __start_xen(). Here's a (big, sorry) code snippet of what is around iommu_setup():

    ...
    init_idle_domain();

    this_cpu(stubs.addr) = alloc_stub_page(smp_processor_id(),
                                           &this_cpu(stubs).mfn);
    BUG_ON(!this_cpu(stubs.addr));

    trap_init();
    rcu_init();

    early_time_init();

    arch_init_memory();

    alternative_instructions();

    local_irq_enable();

    pt_pci_init();

    vesa_mtrr_init();

    acpi_mmcfg_init();

    early_msi_init();

    iommu_setup();    /* setup iommu if available */

    smp_prepare_cpus(max_cpus);

    spin_debug_enable();

    /*
     * Initialise higher-level timer functions. We do this fairly late
     * (after interrupts got enabled) because the time bases and scale
     * factors need to be updated regularly.
     */
    init_xen_time();
    initialize_keytable();
    console_init_postirq();

    system_state = SYS_STATE_smp_boot;
    do_presmp_initcalls();
    for_each_present_cpu ( i )
    {
        /* Set up cpu_to_node[]. */
        srat_detect_node(i);
        /* Set up node_to_cpumask based on cpu_to_node[]. */
        numa_add_cpu(i);

        if ( (num_online_cpus() < max_cpus) && !cpu_online(i) )
        {
            int ret = cpu_up(i);
            if ( ret != 0 )
                printk("Failed to bring up CPU %u (error %d)\n", i, ret);
        }
    }
    printk("Brought up %ld CPUs\n", (long)num_online_cpus());
    ...

As you can see, it is only *after* iommu_setup() that we call functions like smp_prepare_cpus(), do_presmp_initcalls(), and then the loop that waits for all the present CPUs to come online.

Quoted 8 lines

What that means is that, at iommu_setup() time, there still is only one CPU online, and there is not much chances that one single CPU deadlocks in a rendezvous!

Honestly, the biggest issue that I think Quan's patch solves, is that if we ever want/manage to move spin_debug_enable() up above it, then the BUG_ON in check_lock() would trigger the first time that pcidevs_lock would be taken with interrupts enabled.

Right! I got it now.. :-)
The lock is initialized as SPIN_LOCK_UNLOCKED. check_lock() will trigger the BUG_ON when it is used in a interrupt disabled situation.

Quoted 10 lines

Until then, code is technically fine, and, as a matter of fact, I think that removing the locking from that particular instance would be an equally effective fix!

All that being said, consistency is indeed important, and for the sake of it and for other reasons too, even if, strictly speaking, there isn't any actual buggy behavior to be fixed here, and it is worthwhile conforming to a locking pattern that is consistent with the rules that we sat ourselves, unless there's specific reasons not to.

I see. Even though no deadlock may happen, consistency can be the reason to modify. :-)
But it's good to know the real reason of making the change, which could be avoiding the deadlock, be consistency, or both.
It seems to me that this is more for consistency, and avoid the potential deadlock (although not so sure if it will eventually happen, it could be a hazard.).

Thank you, Dario and Quan, very much for your explanation! :-) They are really helpful! :-D

BTW, I'm sorry for the messed format of the table. Let me reformat it here:

Condition 1 (C1): Can run in irq context?

      | spin_lock | spin_lock_irq | spin_lock_irqsave
C1 | No           |  Yes              | Yes
C2 |  No            |  No             | Yes

It may be too fast (incorrect) to get the generalized conclusion, but it should give some overview of the differences of these three locks. Just for reference later. ;-)

Thanks,

Meng

-----------
Meng Xu
PhD Student in Computer and Information Science
University of Pennsylvania
http://www.cis.upenn.edu/~mengxu/

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

#12 lore raw

Re: [PATCH v3 1/2] IOMMU/spinlock: Fix a bug found in AMD IOMMU initialization

In reply to Dario Faggioli

Hi Quan and Dario,

Quoted 151 lines · Dario Faggioli
Quoted 18 lines · Quan
Quoted 8 lines · On March 11, 2016 11:25am, <[email protected]> wrote:
Quoted 6 lines · Quan Xu

pcidevs_lock should be held with interrupt enabled. However there remains an exception in AMD IOMMU code, where the lock is acquired
with interrupt disabled. This inconsistency might lead to deadlock.

Quoted 8 lines

Why will this inconsistency lead to deadlock?
I understand the difference between spin_lock_irqsave(), which disable interrupt,
and spin_lock(), which allows interrupt, however, I didn't get why misuse the
spin_lock_irqsave() at the place of spin_lock() could potentially lead to a
deadlock?

Quoted 7 lines

1).As Jan mentioned, The implication from disabling interrupts while
acquiring a lock is that the lock is also being acquired by some interrupt handler.
If you mix acquire types, the one not disabling interrupts is prone
to be interrupted, and the interrupt trying to get hold of the lock the same CPU already owns.

The key issue is whether or not a lock can be acquired from IRQ context (i.e., in an interrupt handler, or a function called by that, as a result of an interrupt occurring).

For lock that can, IRQ disabling variants must be used *everywhere* the lock is taken (so, e.g., not only when it is actually taken from IRQ context, just *always*!).

If that rule is not honored, we are ok if the lock is taken on CPUA, and the interrupt handled on CPUB:

  CPUA              CPUB
  .                 .
  .                 .
  spin_lock(l)      .
  .                 .
  .                 . <-- Interrupt!
  .                        .
  .                       spin_lock(l); //spins on l
  .                       x             //spins on l
  .                       x             //spins on l
  spin_unlock(l)          .             //takes l
  .                       .
  .                       spin_unlock(l);

. . <-- .
. .

but the following can happen, if the interrupt is delivered to the CPU that has already taken the lock:

     CPU
     .
     .
[1]  spin_lock(l);       //takes l
     .
     . <-- Interrupt!
           .
           spin_lock(l) // CPU deadlocks

If, in the latter case, spin_lock_irq(l) were used at [1], things would have been fine, as the interrupt wouldn't have occurred until l weren't released:

CPU
.
.
spin_lock_irq(l) //takes l
.
. <---------------- Interrupt!

  .                   -                   //IRQs are disabled
  .                   -                   //IRQs are disabled
  .                   -                   //IRQs are disabled
  spin_unlock_irq(l)  .                   //IRQs re-hanbled
                      spin_lock_irq(l);   //takes l
                      .
                      .
                      spin_unlock_irq(l);

. <----------------- .
.

Note that whether spin_lock_irq() or spin_lock_irqsave() should be used, is completely independent from this, and it must be decided according to whether IRQs are disabled already or not when taking the lock. And (quite obviously, but since wre're here) it is fine to mix spin_lock_irq() and spin_lock_irqsave(), as they both disable interrupts, which is what matters.

So, now, if we know for sure that a lock is _never_ever_ever_ taken from interrupt context, can we mix spin_lock() and spin_lock_irq() on it (for whatever reason)? Well, as far as the above reasoning is concerned, yes.

In fact, the deadlock arises because IRQs interrupt asynchronously what the CPU is doing, and that can happen when the CPU has taken the lock already. But if the 'asynchronous' part goes away, we really don't care whether a lock is take at time t1 with IRQ enabled, and at time t2 with IRQ disabled, don't you think?

Well, here it is where what the comment inside check_lock() describes comes into play. As quoted by Qaun already:

Quoted 16 lines

2). Comment inside check_lock(),
we partition locks into IRQ-safe (always held with IRQs disabled) and IRQ-unsafe (always held with IRQs enabled) types. The convention for every lock must be consistently observed else we can deadlock in IRQ-context rendezvous functions (__a rendezvous which gets every CPU into IRQ context before any CPU is released from the rendezvous__). If we can mix IRQ-disabled and IRQ-enabled callers, the following can happen:
* Lock is held by CPU A, with IRQs enabled
* CPU B is spinning on same lock, with IRQs disabled
* Rendezvous starts -- CPU A takes interrupt and enters rendezbous
spin
* DEADLOCK -- CPU B will never enter rendezvous, CPU A will never
exit
the rendezvous, and will hence never release the lock.

Quoted 3 lines

To guard against this subtle bug we latch the IRQ safety of every spinlock in the system, on first use.

This is a very good safety measure, but it can be quite a restriction in some (luckily limited) cases. And that's why it is possible -- although absolutely discouraged-- to relax it. See, for an example of this, the comment in start_secondary(), in xen/arch/x86/smpboot.c:

...
/*
* Just as during early bootstrap, it is convenient here to disable
* spinlock checking while we have IRQs disabled. This allows us to
* acquire IRQ-unsafe locks when it would otherwise be disallowed. *
* It is safe because the race we are usually trying to avoid involves
* a group of CPUs rendezvousing in an IPI handler, where one cannot
* join because it is spinning with IRQs disabled waiting to acquire a
* lock held by another in the rendezvous group (the lock must be an
* IRQ-unsafe lock since the CPU took the IPI after acquiring it, and
* hence had IRQs enabled). This is a deadlock scenario.
*
* However, no CPU can be involved in rendezvous until it is online,
* hence no such group can be waiting for this CPU until it is
* visible in cpu_online_map. Hence such a deadlock is not possible. */
spin_debug_disable();
...

Quoted 4 lines

Just FTR, at least as far as I can remember, Linux does not enforce anything like that.

Hope this clarifies things.

Thank you very much for your explanation and education! They are really helpful! :-D

Let me summarize: ;-)

|                                                      | spin_lock |
spin_lock_irq | spin_lock_irqsave
| Can run in irq context?                  | No            |  Yes
        | Yes
| Can run in irq disabled context?   | No            |  No                | Yes

Why deadlock may occur if we mix the spin_lock and spin_lock_irq(save)? If we mix the spin_lock and spin_lock_irq(save), and a group of CPUs rendezvousing in an IPI handler, we will have deadlock. Because the CPU A that takes spin_lock will wait for the rendezvousing condition to be satisfied, while the CPU B that takes th spin_lock_irq(save) will not enter into the rendezvousing condition (since it disable the interrupt). Then,
CPU A waits for CPU B to handle the IPI to get out of the rendezvousing condition (kind of synchrous point), which won't until it gets the spin_lock.
CPU B waits for CPU A to release the spin_lock, which won't until it get out of the rendezvousing condition;

Are my understanding and summary correct?

Thanks and Best Regards,

Meng

-----------
Meng Xu
PhD Student in Computer and Information Science
University of Pennsylvania
http://www.cis.upenn.edu/~mengxu/

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

#13 lore raw

Re: [PATCH v3 1/2] IOMMU/spinlock: Fix a bug found in AMD IOMMU initialization

Quoted 14 lines · Meng Xu

Thank you very much for your explanation and education! They are really helpful! :-D

Let me summarize: ;-)

Quoted 2 lines

                                                     | spin_lock |

spin_lock_irq | spin_lock_irqsave

Quoted 2 lines

Can run in irq context?                  | No            |  Yes

        | Yes

Quoted 3 lines

Can run in irq disabled context?   |
No            |  No                | Yes

The table came out mangled, at least in my mail reader. For what I can see, I think I see the point you're trying to make, and it's hard to say whether the table itself is right or wrong...

Probably, a table like this, is just not the best way with which to try to represent things.

For instance, you seem to be saying that spin_lock() can't be used if interrupts are disabled, which is not true.

For instance, it is perfectly fine to do the following:

    CPU:
    .
    spin_lock_irq(l1);
    .
    .
[1] spin_lock(l2);
    .
    .
[2] spin_unlock(l2);
    .
    .
    spin_unlock_irq(l1);
    .

Even if l2 is also taken in an interrupt handler. In fact, what we want is make sure that such an interrupt handler that may take l2, does not interrupt CPU in the [1]--[2] time frame... But that can't happen because interrupts are already disabled, so just using spin_lock() is ok, and should be done, as that's cheaper than spin_lock_irqsave().

Fact is, what really matters is whether or not a lock is taken both inside and outside of IRQ context. As a matter of fact, it is mostly the case that, if a lock is ever taken inside an interrupt handler, then spin_lock_irqsave() is what you want... but this does not justify coming up with a table like the one above and claiming it to be general.

Quoted 14 lines

Why deadlock may occur if we mix the spin_lock and spin_lock_irq(save)?
If we mix the spin_lock and spin_lock_irq(save), and a group of CPUs rendezvousing in an IPI handler, we will have deadlock. Because the CPU A that takes spin_lock will wait for the rendezvousing condition to be satisfied, while the CPU B that takes th spin_lock_irq(save) will not enter into the rendezvousing condition (since it disable the interrupt). Then,
CPU A waits for CPU B to handle the IPI to get out of the rendezvousing condition (kind of synchrous point), which won't until it gets the spin_lock.
CPU B waits for CPU A to release the spin_lock, which won't until it get out of the rendezvousing condition;

Quoted 2 lines

Are my understanding and summary correct?

Yes, this looks a decent explanation of the rendezvous-related spinlock problem... Although I prefer the wording that we do have already in check_lock() and in start_secondary(). :-D

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

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

author
#14 lore raw

PATCH v3 2/2 IOMMU/spinlock: Make the pcidevs_lock a recursive one

In reply to Quan Xu
CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Andrew Cooper <[email protected]>
CC Suravee Suthikulpanit <[email protected]>
CC Feng Wu <[email protected]>
CC Kevin Tian <[email protected]>
CC Dario Faggioli <[email protected]>
---
 xen/arch/x86/domctl.c                       |  8 +--
 xen/arch/x86/hvm/vmsi.c                     |  4 +-
 xen/arch/x86/irq.c                          |  8 +--
 xen/arch/x86/msi.c                          | 16 ++---
 xen/arch/x86/pci.c                          |  4 +-
 xen/arch/x86/physdev.c                      | 16 ++---
 xen/common/sysctl.c                         |  4 +-
 xen/drivers/passthrough/amd/iommu_init.c    |  8 +--
 xen/drivers/passthrough/amd/iommu_map.c     |  2 +-
 xen/drivers/passthrough/amd/pci_amd_iommu.c |  4 +-
 xen/drivers/passthrough/pci.c               | 96 +++++++++++++++++------------
 xen/drivers/passthrough/vtd/intremap.c      |  2 +-
 xen/drivers/passthrough/vtd/iommu.c         | 14 ++---
 xen/drivers/video/vga.c                     |  4 +-
 xen/include/xen/pci.h                       |  5 +-
 15 files changed, 109 insertions(+), 86 deletions(-)
xen/arch/x86/domctl.c +4 -4
index 55aecdc..b34a295 100644
@@ -472,9 +472,9 @@ long arch_do_domctl(
         ret = -ESRCH;
         if ( iommu_enabled )
         {
-            spin_lock(&pcidevs_lock);
+            pcidevs_lock();
             ret = pt_irq_create_bind(d, bind);
-            spin_unlock(&pcidevs_lock);
+            pcidevs_unlock();
         }
         if ( ret < 0 )
             printk(XENLOG_G_ERR "pt_irq_create_bind failed (%ld) for dom%d\n",
@@ -497,9 +497,9 @@ long arch_do_domctl(
 
         if ( iommu_enabled )
         {
-            spin_lock(&pcidevs_lock);
+            pcidevs_lock();
             ret = pt_irq_destroy_bind(d, bind);
-            spin_unlock(&pcidevs_lock);
+            pcidevs_unlock();
         }
         if ( ret < 0 )
             printk(XENLOG_G_ERR "pt_irq_destroy_bind failed (%ld) for dom%d\n",
xen/arch/x86/hvm/vmsi.c +2 -2
index 499151e..b41aba5 100644
@@ -388,7 +388,7 @@ int msixtbl_pt_register(struct domain *d, struct pirq *pirq, uint64_t gtable)
     struct msixtbl_entry *entry, *new_entry;
     int r = -EINVAL;
 
-    ASSERT(spin_is_locked(&pcidevs_lock));
+    ASSERT(pcidevs_is_locked());
     ASSERT(spin_is_locked(&d->event_lock));
 
     if ( !has_vlapic(d) )
@@ -446,7 +446,7 @@ void msixtbl_pt_unregister(struct domain *d, struct pirq *pirq)
     struct pci_dev *pdev;
     struct msixtbl_entry *entry;
 
-    ASSERT(spin_is_locked(&pcidevs_lock));
+    ASSERT(pcidevs_is_locked());
     ASSERT(spin_is_locked(&d->event_lock));
 
     if ( !has_vlapic(d) )
xen/arch/x86/irq.c +4 -4
index 1228568..78e4263 100644
@@ -1960,7 +1960,7 @@ int map_domain_pirq(
         struct pci_dev *pdev;
         unsigned int nr = 0;
 
-        ASSERT(spin_is_locked(&pcidevs_lock));
+        ASSERT(pcidevs_is_locked());
 
         ret = -ENODEV;
         if ( !cpu_has_apic )
@@ -2105,7 +2105,7 @@ int unmap_domain_pirq(struct domain *d, int pirq)
     if ( (pirq < 0) || (pirq >= d->nr_pirqs) )
         return -EINVAL;
 
-    ASSERT(spin_is_locked(&pcidevs_lock));
+    ASSERT(pcidevs_is_locked());
     ASSERT(spin_is_locked(&d->event_lock));
 
     info = pirq_info(d, pirq);
@@ -2231,7 +2231,7 @@ void free_domain_pirqs(struct domain *d)
 {
     int i;
 
-    spin_lock(&pcidevs_lock);
+    pcidevs_lock();
     spin_lock(&d->event_lock);
 
     for ( i = 0; i < d->nr_pirqs; i++ )
@@ -2239,7 +2239,7 @@ void free_domain_pirqs(struct domain *d)
             unmap_domain_pirq(d, i);
 
     spin_unlock(&d->event_lock);
-    spin_unlock(&pcidevs_lock);
+    pcidevs_unlock();
 }
 
 static void dump_irqs(unsigned char key)
xen/arch/x86/msi.c +8 -8
index 5a481f6..a6018a2 100644
@@ -694,7 +694,7 @@ static int msi_capability_init(struct pci_dev *dev,
     u8 slot = PCI_SLOT(dev->devfn);
     u8 func = PCI_FUNC(dev->devfn);
 
-    ASSERT(spin_is_locked(&pcidevs_lock));
+    ASSERT(pcidevs_is_locked());
     pos = pci_find_cap_offset(seg, bus, slot, func, PCI_CAP_ID_MSI);
     if ( !pos )
         return -ENODEV;
@@ -852,7 +852,7 @@ static int msix_capability_init(struct pci_dev *dev,
     u8 func = PCI_FUNC(dev->devfn);
     bool_t maskall = msix->host_maskall;
 
-    ASSERT(spin_is_locked(&pcidevs_lock));
+    ASSERT(pcidevs_is_locked());
 
     control = pci_conf_read16(seg, bus, slot, func, msix_control_reg(pos));
     /*
@@ -1042,7 +1042,7 @@ static int __pci_enable_msi(struct msi_info *msi, struct msi_desc **desc)
     struct pci_dev *pdev;
     struct msi_desc *old_desc;
 
-    ASSERT(spin_is_locked(&pcidevs_lock));
+    ASSERT(pcidevs_is_locked());
     pdev = pci_get_pdev(msi->seg, msi->bus, msi->devfn);
     if ( !pdev )
         return -ENODEV;
@@ -1103,7 +1103,7 @@ static int __pci_enable_msix(struct msi_info *msi, struct msi_desc **desc)
     u8 func = PCI_FUNC(msi->devfn);
     struct msi_desc *old_desc;
 
-    ASSERT(spin_is_locked(&pcidevs_lock));
+    ASSERT(pcidevs_is_locked());
     pdev = pci_get_pdev(msi->seg, msi->bus, msi->devfn);
     pos = pci_find_cap_offset(msi->seg, msi->bus, slot, func, PCI_CAP_ID_MSIX);
     if ( !pdev || !pos )
@@ -1205,7 +1205,7 @@ int pci_prepare_msix(u16 seg, u8 bus, u8 devfn, bool_t off)
     if ( !pos )
         return -ENODEV;
 
-    spin_lock(&pcidevs_lock);
+    pcidevs_lock();
     pdev = pci_get_pdev(seg, bus, devfn);
     if ( !pdev )
         rc = -ENODEV;
@@ -1224,7 +1224,7 @@ int pci_prepare_msix(u16 seg, u8 bus, u8 devfn, bool_t off)
         rc = msix_capability_init(pdev, pos, NULL, NULL,
                                   multi_msix_capable(control));
     }
-    spin_unlock(&pcidevs_lock);
+    pcidevs_unlock();
 
     return rc;
 }
@@ -1235,7 +1235,7 @@ int pci_prepare_msix(u16 seg, u8 bus, u8 devfn, bool_t off)
  */
 int pci_enable_msi(struct msi_info *msi, struct msi_desc **desc)
 {
-    ASSERT(spin_is_locked(&pcidevs_lock));
+    ASSERT(pcidevs_is_locked());
 
     if ( !use_msi )
         return -EPERM;
@@ -1351,7 +1351,7 @@ int pci_restore_msi_state(struct pci_dev *pdev)
     unsigned int type = 0, pos = 0;
     u16 control = 0;
 
-    ASSERT(spin_is_locked(&pcidevs_lock));
+    ASSERT(pcidevs_is_locked());
 
     if ( !use_msi )
         return -EOPNOTSUPP;
xen/arch/x86/pci.c +2 -2
index 4b87cab..a9decd4 100644
@@ -88,13 +88,13 @@ int pci_conf_write_intercept(unsigned int seg, unsigned int bdf,
     if ( reg < 64 || reg >= 256 )
         return 0;
 
-    spin_lock(&pcidevs_lock);
+    pcidevs_lock();
 
     pdev = pci_get_pdev(seg, PCI_BUS(bdf), PCI_DEVFN2(bdf));
     if ( pdev )
         rc = pci_msi_conf_write_intercept(pdev, reg, size, data);
 
-    spin_unlock(&pcidevs_lock);
+    pcidevs_unlock();
 
     return rc;
 }
xen/arch/x86/physdev.c +8 -8
index 57b7800..1cb9b58 100644
@@ -167,7 +167,7 @@ int physdev_map_pirq(domid_t domid, int type, int *index, int *pirq_p,
         goto free_domain;
     }
 
-    spin_lock(&pcidevs_lock);
+    pcidevs_lock();
     /* Verify or get pirq. */
     spin_lock(&d->event_lock);
     pirq = domain_irq_to_pirq(d, irq);
@@ -237,7 +237,7 @@ int physdev_map_pirq(domid_t domid, int type, int *index, int *pirq_p,
 
  done:
     spin_unlock(&d->event_lock);
-    spin_unlock(&pcidevs_lock);
+    pcidevs_unlock();
     if ( ret != 0 )
         switch ( type )
         {
@@ -275,11 +275,11 @@ int physdev_unmap_pirq(domid_t domid, int pirq)
             goto free_domain;
     }
 
-    spin_lock(&pcidevs_lock);
+    pcidevs_lock();
     spin_lock(&d->event_lock);
     ret = unmap_domain_pirq(d, pirq);
     spin_unlock(&d->event_lock);
-    spin_unlock(&pcidevs_lock);
+    pcidevs_unlock();
 
  free_domain:
     rcu_unlock_domain(d);
@@ -689,10 +689,10 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         if ( copy_from_guest(&restore_msi, arg, 1) != 0 )
             break;
 
-        spin_lock(&pcidevs_lock);
+        pcidevs_lock();
         pdev = pci_get_pdev(0, restore_msi.bus, restore_msi.devfn);
         ret = pdev ? pci_restore_msi_state(pdev) : -ENODEV;
-        spin_unlock(&pcidevs_lock);
+        pcidevs_unlock();
         break;
     }
 
@@ -704,10 +704,10 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         if ( copy_from_guest(&dev, arg, 1) != 0 )
             break;
 
-        spin_lock(&pcidevs_lock);
+        pcidevs_lock();
         pdev = pci_get_pdev(dev.seg, dev.bus, dev.devfn);
         ret = pdev ? pci_restore_msi_state(pdev) : -ENODEV;
-        spin_unlock(&pcidevs_lock);
+        pcidevs_unlock();
         break;
     }
 
xen/common/sysctl.c +2 -2
index 58162f5..253b7c8 100644
@@ -426,7 +426,7 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl)
                 break;
             }
 
-            spin_lock(&pcidevs_lock);
+            pcidevs_lock();
             pdev = pci_get_pdev(dev.seg, dev.bus, dev.devfn);
             if ( !pdev )
                 node = XEN_INVALID_DEV;
@@ -434,7 +434,7 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl)
                 node = XEN_INVALID_NODE_ID;
             else
                 node = pdev->node;
-            spin_unlock(&pcidevs_lock);
+            pcidevs_unlock();
 
             if ( copy_to_guest_offset(ti->nodes, i, &node, 1) )
             {
xen/drivers/passthrough/amd/iommu_init.c +4 -4
index a400497..4536106 100644
@@ -673,9 +673,9 @@ void parse_ppr_log_entry(struct amd_iommu *iommu, u32 entry[])
     bus = PCI_BUS(device_id);
     devfn = PCI_DEVFN2(device_id);
 
-    spin_lock(&pcidevs_lock);
+    pcidevs_lock();
     pdev = pci_get_real_pdev(iommu->seg, bus, devfn);
-    spin_unlock(&pcidevs_lock);
+    pcidevs_unlock();
 
     if ( pdev )
         guest_iommu_add_ppr_log(pdev->domain, entry);
@@ -787,10 +787,10 @@ static bool_t __init set_iommu_interrupt_handler(struct amd_iommu *iommu)
         return 0;
     }
 
-    spin_lock(&pcidevs_lock);
+    pcidevs_lock();
     iommu->msi.dev = pci_get_pdev(iommu->seg, PCI_BUS(iommu->bdf),
                                   PCI_DEVFN2(iommu->bdf));
-    spin_unlock(&pcidevs_lock);
+    pcidevs_unlock();
     if ( !iommu->msi.dev )
     {
         AMD_IOMMU_DEBUG("IOMMU: no pdev for %04x:%02x:%02x.%u\n",
xen/drivers/passthrough/amd/iommu_map.c +1 -1
index 78862c9..9d91dfc 100644
@@ -593,7 +593,7 @@ static int update_paging_mode(struct domain *d, unsigned long gfn)
         hd->arch.paging_mode = level;
         hd->arch.root_table = new_root;
 
-        if ( !spin_is_locked(&pcidevs_lock) )
+        if ( !pcidevs_is_locked() )
             AMD_IOMMU_DEBUG("%s Try to access pdev_list "
                             "without aquiring pcidevs_lock.\n", __func__);
 
xen/drivers/passthrough/amd/pci_amd_iommu.c +2 -2
index c1c0b6b..dc3bfab 100644
@@ -158,7 +158,7 @@ static void amd_iommu_setup_domain_device(
 
     spin_unlock_irqrestore(&iommu->lock, flags);
 
-    ASSERT(spin_is_locked(&pcidevs_lock));
+    ASSERT(pcidevs_is_locked());
 
     if ( pci_ats_device(iommu->seg, bus, pdev->devfn) &&
          !pci_ats_enabled(iommu->seg, bus, pdev->devfn) )
@@ -345,7 +345,7 @@ void amd_iommu_disable_domain_device(struct domain *domain,
     }
     spin_unlock_irqrestore(&iommu->lock, flags);
 
-    ASSERT(spin_is_locked(&pcidevs_lock));
+    ASSERT(pcidevs_is_locked());
 
     if ( devfn == pdev->devfn &&
          pci_ats_device(iommu->seg, bus, devfn) &&
xen/drivers/passthrough/pci.c +58 -38

285 lines not on this page · raw message

xen/drivers/passthrough/vtd/intremap.c +1 -1
index 0ee3fb2..f5e3f49 100644
@@ -984,7 +984,7 @@ int pi_update_irte(const struct vcpu *v, const struct pirq *pirq,
 
     spin_unlock_irq(&desc->lock);
 
-    ASSERT(spin_is_locked(&pcidevs_lock));
+    ASSERT(pcidevs_is_locked());
 
     /*
      * FIXME: For performance reasons we should store the 'iommu' pointer in
xen/drivers/passthrough/vtd/iommu.c +7 -7
index 8022702..8409100 100644
@@ -1282,7 +1282,7 @@ int domain_context_mapping_one(
     u16 seg = iommu->intel->drhd->segment;
     int agaw;
 
-    ASSERT(spin_is_locked(&pcidevs_lock));
+    ASSERT(pcidevs_is_locked());
     spin_lock(&iommu->lock);
     maddr = bus_to_context_maddr(iommu, bus);
     context_entries = (struct context_entry *)map_vtd_domain_page(maddr);
@@ -1424,7 +1424,7 @@ static int domain_context_mapping(
     if ( !drhd )
         return -ENODEV;
 
-    ASSERT(spin_is_locked(&pcidevs_lock));
+    ASSERT(pcidevs_is_locked());
 
     switch ( pdev->type )
     {
@@ -1506,7 +1506,7 @@ int domain_context_unmap_one(
     u64 maddr;
     int iommu_domid;
 
-    ASSERT(spin_is_locked(&pcidevs_lock));
+    ASSERT(pcidevs_is_locked());
     spin_lock(&iommu->lock);
 
     maddr = bus_to_context_maddr(iommu, bus);
@@ -1816,7 +1816,7 @@ static int rmrr_identity_mapping(struct domain *d, bool_t map,
     struct mapped_rmrr *mrmrr;
     struct hvm_iommu *hd = domain_hvm_iommu(d);
 
-    ASSERT(spin_is_locked(&pcidevs_lock));
+    ASSERT(pcidevs_is_locked());
     ASSERT(rmrr->base_address < rmrr->end_address);
 
     /*
@@ -1881,7 +1881,7 @@ static int intel_iommu_add_device(u8 devfn, struct pci_dev *pdev)
     u16 bdf;
     int ret, i;
 
-    ASSERT(spin_is_locked(&pcidevs_lock));
+    ASSERT(pcidevs_is_locked());
 
     if ( !pdev->domain )
         return -EINVAL;
@@ -2109,7 +2109,7 @@ static void __hwdom_init setup_hwdom_rmrr(struct domain *d)
     u16 bdf;
     int ret, i;
 
-    spin_lock(&pcidevs_lock);
+    pcidevs_lock();
     for_each_rmrr_device ( rmrr, bdf, i )
     {
         /*
@@ -2123,7 +2123,7 @@ static void __hwdom_init setup_hwdom_rmrr(struct domain *d)
             dprintk(XENLOG_ERR VTDPREFIX,
                      "IOMMU: mapping reserved region failed\n");
     }
-    spin_unlock(&pcidevs_lock);
+    pcidevs_unlock();
 }
 
 int __init intel_vtd_setup(void)
xen/drivers/video/vga.c +2 -2
index 40e5963..61c6b13 100644
@@ -117,9 +117,9 @@ void __init video_endboot(void)
                 const struct pci_dev *pdev;
                 u8 b = bus, df = devfn, sb;
 
-                spin_lock(&pcidevs_lock);
+                pcidevs_lock();
                 pdev = pci_get_pdev(0, bus, devfn);
-                spin_unlock(&pcidevs_lock);
+                pcidevs_unlock();
 
                 if ( !pdev ||
                      pci_conf_read16(0, bus, PCI_SLOT(devfn), PCI_FUNC(devfn),
xen/include/xen/pci.h +4 -1
index a5aef55..b87571d 100644
@@ -94,7 +94,10 @@ struct pci_dev {
  * interrupt handling related (the mask bit register).
  */
 
-extern spinlock_t pcidevs_lock;
+void pcidevs_lock(void);
+void pcidevs_unlock(void);
+int pcidevs_is_locked(void);
+int pcidevs_trylock(void);
 
 bool_t pci_known_segment(u16 seg);
 bool_t pci_device_detect(u16 seg, u8 bus, u8 dev, u8 func);
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Reviewed-by on v3 2/2
#15 lore raw

Re: [PATCH v3 2/2] IOMMU/spinlock: Make the pcidevs_lock a recursive one

Quoted 3 lines · Quan Xu

And I've applied and build tested it, against current staging, and this time, it worked. :-)

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

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

author
#16 lore raw

Re: [PATCH v3 2/2] IOMMU/spinlock: Make the pcidevs_lock a recursive one

Quoted 9 lines · On March 10, 2016 1:45am, <[email protected]> wrote:
Quoted 3 lines · Quan Xu

And I've applied and build tested it, against current staging, and this time, it worked. :-)

Dario, thanks!! :):)
Quan

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

#17 lore raw

Re: [PATCH v3 2/2] IOMMU/spinlock: Make the pcidevs_lock a recursive one

In reply to Quan Xu
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 09.03.16 at 14:17, <[email protected]> wrote:

The patch itself looks mostly fine now (see below for the minor left issues), but the complete lack of a description (which should state why this change is being done) makes this not ready to go in anyway.

xen/drivers/passthrough/pci.c struct pci_seg { · line 51

Quoted 8 lines
xen/drivers/passthrough/pci.c +1 -1
@@ -48,7 +48,7 @@ struct pci_seg {
     } bus2bridge[MAX_BUSES];
 };
 
-spinlock_t pcidevs_lock = SPIN_LOCK_UNLOCKED;
+static spinlock_t _pcidevs_lock = SPIN_LOCK_UNLOCKED;

Why the renaming?

Quoted 5 lines
xen/drivers/passthrough/pci.c +1 -0
@@ -103,6 +103,26 @@ static int pci_segments_iterate(
     return rc;
 }
 
+void pcidevs_lock(void)

xen/drivers/passthrough/pci.c static int pci_segments_iterate( · line 116

Quoted 10 lines
xen/drivers/passthrough/pci.c +10 -0
+{
+    spin_lock_recursive(&_pcidevs_lock);
+}
+
+void pcidevs_unlock(void)
+{
+    spin_unlock_recursive(&_pcidevs_lock);
+}
+
+int pcidevs_is_locked(void)

bool_t

xen/drivers/passthrough/pci.c static int pci_segments_iterate( · line 121

Quoted 5 lines
xen/drivers/passthrough/pci.c +5 -0
+{
+    return spin_is_locked(&_pcidevs_lock);
+}
+
+int pcidevs_trylock(void)

bool_t

(To avoid another round, please be aware that the underlying spin lock primitives still [wrongly] use "int", so to be fully correct you will need to use !! in both return statements, unless you feel like (in another prereq patch) to adjust those primitives too.

xen/drivers/passthrough/pci.c static int pci_segments_iterate( · line 124

Quoted 3 lines
xen/drivers/passthrough/pci.c +3 -0
+{
+    return spin_trylock_recursive(&_pcidevs_lock);
+}

I also think that it would be a good idea to place these helpers and the lock definition next to each other.

Jan

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

author
#18 lore raw

Re: [PATCH v3 2/2] IOMMU/spinlock: Make the pcidevs_lock a recursive one

Quoted 8 lines · On March 10, 2016 5:53pm, <March 10, 2016 5:53> wrote:
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 09.03.16 at 14:17, <[email protected]> wrote:

The patch itself looks mostly fine now (see below for the minor left issues), but the complete lack of a description (which should state why this change is being done) makes this not ready to go in anyway.

What about the following description:
--
The pcidevs_lock may be recursively taken for hiding ATS device, when VT-d Device-TLB flush timed out.

Quoted 11 lines

xen/drivers/passthrough/pci.c struct pci_seg { · line 51

Quoted 8 lines
xen/drivers/passthrough/pci.c +1 -1
@@ -48,7 +48,7 @@ struct pci_seg {
     } bus2bridge[MAX_BUSES];
 };
 
-spinlock_t pcidevs_lock = SPIN_LOCK_UNLOCKED;
+static spinlock_t _pcidevs_lock = SPIN_LOCK_UNLOCKED;

Why the renaming?

The original name would cause compiler errors, as I also introduce a new helper with same name -- void pcidevs_lock(void).

Quoted 25 lines
Quoted 5 lines
xen/drivers/passthrough/pci.c +1 -0
@@ -103,6 +103,26 @@ static int pci_segments_iterate(
     return rc;
 }
 
+void pcidevs_lock(void)

xen/drivers/passthrough/pci.c static int pci_segments_iterate( · line 116

Quoted 10 lines
xen/drivers/passthrough/pci.c +10 -0
+{
+    spin_lock_recursive(&_pcidevs_lock);
+}
+
+void pcidevs_unlock(void)
+{
+    spin_unlock_recursive(&_pcidevs_lock);
+}
+
+int pcidevs_is_locked(void)

bool_t

xen/drivers/passthrough/pci.c static int pci_segments_iterate( · line 121

Quoted 4 lines
xen/drivers/passthrough/pci.c +4 -0
+{
+    return spin_is_locked(&_pcidevs_lock); }
+
+int pcidevs_trylock(void)

bool_t

Quoted 5 lines

(To avoid another round, please be aware that the underlying spin lock primitives still [wrongly] use "int", so to be fully correct you will need to use !! in both return statements, unless you feel like (in another prereq patch) to adjust those primitives too.

Thanks for reminding.
I'd prefer to use !! in both return statements.
btw, could you help me check the code style?:

+bool_t pcidevs_is_locked(void)
+{
+ return !!(spin_is_locked(&_pcidevs_lock));
+}
+
+bool_t pcidevs_trylock(void)
+{
+ return !!(spin_trylock_recursive(&_pcidevs_lock));
+}

Is it right?

Quoted 7 lines

xen/drivers/passthrough/pci.c static int pci_segments_iterate( · line 124

Quoted 3 lines
xen/drivers/passthrough/pci.c +3 -0
+{
+    return spin_trylock_recursive(&_pcidevs_lock);
+}

I also think that it would be a good idea to place these helpers and the lock definition next to each other.

Agreed, make sense.

Quan

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

#19 lore raw

Re: [PATCH v3 2/2] IOMMU/spinlock: Make the pcidevs_lock a recursive one

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 10.03.16 at 12:27, <[email protected]> wrote:
Quoted 5 lines · On March 10, 2016 5:53pm, <March 10, 2016 5:53> wrote:
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 09.03.16 at 14:17, <[email protected]> wrote:

The patch itself looks mostly fine now (see below for the minor left

issues), but

Quoted 1 line

the complete lack of a description (which should state why this change is

Quoted 8 lines

being

Quoted 2 lines

done) makes this not ready to go in anyway.

What about the following description:
--
The pcidevs_lock may be recursively taken for hiding ATS device, when VT-d Device-TLB flush timed out.

s/may/is going to/ (since right now that isn't the case yet)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
xen/drivers/passthrough/pci.c +0 -0
@@ -48,7 +48,7 @@ struct pci_seg {
     } bus2bridge[MAX_BUSES];
 };
 
Quoted 8 lines
Quoted 5 lines

xen/drivers/passthrough/pci.c struct pci_seg { · line 51

Quoted 2 lines
xen/drivers/passthrough/pci.c +1 -1
-spinlock_t pcidevs_lock = SPIN_LOCK_UNLOCKED;
+static spinlock_t _pcidevs_lock = SPIN_LOCK_UNLOCKED;

Why the renaming?

The original name would cause compiler errors, as I also introduce a new helper with same name -- void pcidevs_lock(void).

Ah, of course.

Quoted 46 lines
Quoted 25 lines
Quoted 5 lines
xen/drivers/passthrough/pci.c +1 -0
@@ -103,6 +103,26 @@ static int pci_segments_iterate(
     return rc;
 }
 
+void pcidevs_lock(void)

xen/drivers/passthrough/pci.c static int pci_segments_iterate( · line 116

Quoted 10 lines
xen/drivers/passthrough/pci.c +10 -0
+{
+    spin_lock_recursive(&_pcidevs_lock);
+}
+
+void pcidevs_unlock(void)
+{
+    spin_unlock_recursive(&_pcidevs_lock);
+}
+
+int pcidevs_is_locked(void)

bool_t

xen/drivers/passthrough/pci.c static int pci_segments_iterate( · line 121

Quoted 4 lines
xen/drivers/passthrough/pci.c +4 -0
+{
+    return spin_is_locked(&_pcidevs_lock); }
+
+int pcidevs_trylock(void)

bool_t

Quoted 2 lines

(To avoid another round, please be aware that the underlying spin lock primitives still [wrongly] use "int", so to be fully correct you will need

to use !! in

Quoted 1 line

both return statements, unless you feel like (in another prereq patch) to

adjust

Quoted 2 lines

those primitives too.

Thanks for reminding.
I'd prefer to use !! in both return statements.
btw, could you help me check the code style?:

xen/drivers/passthrough/pci.c +9 -0
+bool_t pcidevs_is_locked(void)
+{
+    return !!(spin_is_locked(&_pcidevs_lock));
+}
+
+bool_t pcidevs_trylock(void)
+{
+    return !!(spin_trylock_recursive(&_pcidevs_lock));
+}
Quoted 1 line

Is it right?

The outermost parentheses aren't needed, and harm readability.

Jan

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