Skip to contentSkip to conversation

PCI: Fix potential deadlock in pcim_intx()

PATCH In review Philipp Stanner 3 participants 5 messages Sep 5, 2024 – Sep 12, 2024 PCI

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 PCI: Fix potential deadlock in pcim_intx()

commit 25216afc9db5 ("PCI: Add managed pcim_intx()") moved the allocation step for pci_intx()'s device resource from pcim_enable_device() to pcim_intx(). As before, pcim_enable_device() sets pci_dev.is_managed to true; and it is never set to false again.

Due to the lifecycle of a struct pci_dev, it can happen that a second driver obtains the same pci_dev after a first driver ran.
If one driver uses pcim_enable_device() and the other doesn't, this causes the other driver to run into managed pcim_intx(), which will try to allocate when called for the first time.

Allocations might sleep, so calling pci_intx() while holding spinlocks becomes then invalid, which causes lockdep warnings and could cause deadlocks:

========================================================
WARNING: possible irq lock inversion dependency detected
6.11.0-rc6+ #59 Tainted: G W
--------------------------------------------------------
CPU 0/KVM/1537 just changed the state of lock:
ffffa0f0cff965f0 (&vdev->irqlock){-...}-{2:2}, at:
vfio_intx_handler+0x21/0xd0 [vfio_pci_core] but this lock took another, HARDIRQ-unsafe lock in the past: (fs_reclaim){+.+.}-{0:0}

and interrupts could create inverse lock ordering between them.

other info that might help us debug this:
Possible interrupt unsafe locking scenario:

       CPU0                    CPU1
       ----                    ----
  lock(fs_reclaim);
                               local_irq_disable();
                               lock(&vdev->irqlock);
                               lock(fs_reclaim);
  <Interrupt>
    lock(&vdev->irqlock);

*** DEADLOCK ***

Have pcim_enable_device()'s release function, pcim_disable_device(), set pci_dev.is_managed to false so that subsequent drivers using the same struct pci_dev do implicitly run into managed code.

---
@Bjorn:
This problem was introduced in the v6.11 merge window. So one might consider getting it into mainline before v6.11.0 gets tagged.

P.

---
 drivers/pci/devres.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/pci/devres.c +2 -0
index 3780a9f9ec00..c7affbbf73ab 100644
@@ -483,6 +483,8 @@ static void pcim_disable_device(void *pdev_raw)
 
 	if (!pdev->pinned)
 		pci_disable_device(pdev);
+
+	pdev->is_managed = false;
 }
 
 /**
Signature · 1 line
2.46.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 44 lines · Philipp Stanner

commit 25216afc9db5 ("PCI: Add managed pcim_intx()") moved the allocation step for pci_intx()'s device resource from pcim_enable_device() to pcim_intx(). As before, pcim_enable_device() sets pci_dev.is_managed to true; and it is never set to false again.

Due to the lifecycle of a struct pci_dev, it can happen that a second driver obtains the same pci_dev after a first driver ran.
If one driver uses pcim_enable_device() and the other doesn't, this causes the other driver to run into managed pcim_intx(), which will try to allocate when called for the first time.

Allocations might sleep, so calling pci_intx() while holding spinlocks becomes then invalid, which causes lockdep warnings and could cause deadlocks:

========================================================
WARNING: possible irq lock inversion dependency detected
6.11.0-rc6+ #59 Tainted: G W
--------------------------------------------------------
CPU 0/KVM/1537 just changed the state of lock:
ffffa0f0cff965f0 (&vdev->irqlock){-...}-{2:2}, at:
vfio_intx_handler+0x21/0xd0 [vfio_pci_core] but this lock took another, HARDIRQ-unsafe lock in the past: (fs_reclaim){+.+.}-{0:0}

and interrupts could create inverse lock ordering between them.

other info that might help us debug this:
Possible interrupt unsafe locking scenario:

       CPU0                    CPU1
       ----                    ----
  lock(fs_reclaim);
                               local_irq_disable();
                               lock(&vdev->irqlock);
                               lock(fs_reclaim);
  <Interrupt>
    lock(&vdev->irqlock);

*** DEADLOCK ***

Have pcim_enable_device()'s release function, pcim_disable_device(), set pci_dev.is_managed to false so that subsequent drivers using the same struct pci_dev do implicitly run into managed code.

Quoted 6 lines

Looks OK to me.

Signature · 2 lines
Damien Le Moal
Western Digital Research
#2 lore raw
In reply to Philipp Stanner
Quoted 50 lines · Philipp Stanner

commit 25216afc9db5 ("PCI: Add managed pcim_intx()") moved the allocation step for pci_intx()'s device resource from pcim_enable_device() to pcim_intx(). As before, pcim_enable_device() sets pci_dev.is_managed to true; and it is never set to false again.

Due to the lifecycle of a struct pci_dev, it can happen that a second driver obtains the same pci_dev after a first driver ran.
If one driver uses pcim_enable_device() and the other doesn't, this causes the other driver to run into managed pcim_intx(), which will try to allocate when called for the first time.

Allocations might sleep, so calling pci_intx() while holding spinlocks becomes then invalid, which causes lockdep warnings and could cause deadlocks:

========================================================
WARNING: possible irq lock inversion dependency detected
6.11.0-rc6+ #59 Tainted: G W
--------------------------------------------------------
CPU 0/KVM/1537 just changed the state of lock:
ffffa0f0cff965f0 (&vdev->irqlock){-...}-{2:2}, at:
vfio_intx_handler+0x21/0xd0 [vfio_pci_core] but this lock took another, HARDIRQ-unsafe lock in the past: (fs_reclaim){+.+.}-{0:0}

and interrupts could create inverse lock ordering between them.

other info that might help us debug this:
Possible interrupt unsafe locking scenario:

       CPU0                    CPU1
       ----                    ----
  lock(fs_reclaim);
                               local_irq_disable();
                               lock(&vdev->irqlock);
                               lock(fs_reclaim);
  <Interrupt>
    lock(&vdev->irqlock);

*** DEADLOCK ***

Have pcim_enable_device()'s release function, pcim_disable_device(), set pci_dev.is_managed to false so that subsequent drivers using the same struct pci_dev do implicitly run into managed code.

Quoted 4 lines

---
@Bjorn:
This problem was introduced in the v6.11 merge window. So one might consider getting it into mainline before v6.11.0 gets tagged.

Applied with Damien's Reviewed-by to pci/for-linus for v6.11, thanks.

Quoted 20 lines
---
 drivers/pci/devres.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/pci/devres.c +2 -0
index 3780a9f9ec00..c7affbbf73ab 100644
@@ -483,6 +483,8 @@ static void pcim_disable_device(void *pdev_raw)
 
 	if (!pdev->pinned)
 		pci_disable_device(pdev);
+
+	pdev->is_managed = false;
 }
 
 /**
Signature · 1 line
2.46.0
#3 lore raw
Quoted 45 lines · Bjorn Helgaas
Quoted 44 lines · Philipp Stanner

commit 25216afc9db5 ("PCI: Add managed pcim_intx()") moved the allocation step for pci_intx()'s device resource from pcim_enable_device() to pcim_intx(). As before, pcim_enable_device()
sets pci_dev.is_managed to true; and it is never set to false again.

Due to the lifecycle of a struct pci_dev, it can happen that a second
driver obtains the same pci_dev after a first driver ran. If one driver uses pcim_enable_device() and the other doesn't, this causes the other driver to run into managed pcim_intx(), which will
try to allocate when called for the first time.

Allocations might sleep, so calling pci_intx() while holding spinlocks
becomes then invalid, which causes lockdep warnings and could cause deadlocks:

========================================================
WARNING: possible irq lock inversion dependency detected 6.11.0-rc6+ #59 Tainted: G        W
--------------------------------------------------------
CPU 0/KVM/1537 just changed the state of lock:
ffffa0f0cff965f0 (&vdev->irqlock){-...}-{2:2}, at:
vfio_intx_handler+0x21/0xd0 [vfio_pci_core] but this lock took another,
HARDIRQ-unsafe lock in the past: (fs_reclaim){+.+.}-{0:0}

and interrupts could create inverse lock ordering between them.

other info that might help us debug this:
 Possible interrupt unsafe locking scenario:

       CPU0                    CPU1
       ----                    ---- lock(fs_reclaim);
                               local_irq_disable(); lock(&vdev->irqlock); lock(fs_reclaim); <Interrupt>
    lock(&vdev->irqlock);

Quoted 7 lines
Quoted 7 lines

 *** DEADLOCK ***

Have pcim_enable_device()'s release function, pcim_disable_device(), set
pci_dev.is_managed to false so that subsequent drivers using the same
struct pci_dev do implicitly run into managed code.

Oops, that should obviously be "do *not* run into managed code."

Mea culpa. Maybe you can ammend that, Bjorn?

Quoted 13 lines
Quoted 5 lines
Fixes 25216afc9db5 ("PCI: Add managed pcim_intx()")

Closes:
https://lore.kernel.org/all/[email protected]/

Quoted 7 lines

---
@Bjorn:
This problem was introduced in the v6.11 merge window. So one might consider getting it into mainline before v6.11.0 gets tagged.

Quoted 1 line

Applied with Damien's Reviewed-by to pci/for-linus for v6.11, thanks.

thx!

P.

Quoted 22 lines
Quoted 21 lines

---
 drivers/pci/devres.c | 2 ++

 1 file changed, 2 insertions(+)
drivers/pci/devres.c +0 -0
index 3780a9f9ec00..c7affbbf73ab 100644
@@ -483,6 +483,8 @@ static void pcim_disable_device(void *pdev_raw)


  if (!pdev->pinned)
  pci_disable_device(pdev);

drivers/pci/devres.c +2 -0
+
+	pdev->is_managed = false;

 }

 /**

Signature · 1 line
2.46.0
#4 lore raw
Quoted 54 lines · Philipp Stanner
Quoted 45 lines · Bjorn Helgaas
Quoted 44 lines · Philipp Stanner

commit 25216afc9db5 ("PCI: Add managed pcim_intx()") moved the allocation step for pci_intx()'s device resource from pcim_enable_device() to pcim_intx(). As before, pcim_enable_device()
sets pci_dev.is_managed to true; and it is never set to false again.

Due to the lifecycle of a struct pci_dev, it can happen that a second
driver obtains the same pci_dev after a first driver ran. If one driver uses pcim_enable_device() and the other doesn't, this causes the other driver to run into managed pcim_intx(), which will
try to allocate when called for the first time.

Allocations might sleep, so calling pci_intx() while holding spinlocks
becomes then invalid, which causes lockdep warnings and could cause deadlocks:

========================================================
WARNING: possible irq lock inversion dependency detected 6.11.0-rc6+ #59 Tainted: G        W
--------------------------------------------------------
CPU 0/KVM/1537 just changed the state of lock:
ffffa0f0cff965f0 (&vdev->irqlock){-...}-{2:2}, at:
vfio_intx_handler+0x21/0xd0 [vfio_pci_core] but this lock took another,
HARDIRQ-unsafe lock in the past: (fs_reclaim){+.+.}-{0:0}

and interrupts could create inverse lock ordering between them.

other info that might help us debug this:
 Possible interrupt unsafe locking scenario:

       CPU0                    CPU1
       ----                    ---- lock(fs_reclaim);
                               local_irq_disable(); lock(&vdev->irqlock); lock(fs_reclaim); <Interrupt>
    lock(&vdev->irqlock);

Quoted 7 lines
Quoted 7 lines

 *** DEADLOCK ***

Have pcim_enable_device()'s release function, pcim_disable_device(), set
pci_dev.is_managed to false so that subsequent drivers using the same
struct pci_dev do implicitly run into managed code.

Quoted 3 lines

Oops, that should obviously be "do *not* run into managed code."

Mea culpa. Maybe you can ammend that, Bjorn?

Fixed, thanks for the pointer.